bugGNU Octave - Bugs: bug #43038, 'mean' fails when operating on...

 
 

bug #43038: 'mean' fails when operating on trailing singleton dimension

Submitted by:  None
Submitted on:  Thu 21 Aug 2014 02:41:34 AM UTC  
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Pierre Bellec Originator Email:  -email is unavailable-
Open/Closed:  Closed Release:  dev
Operating System:  GNU/Linux

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

( Jump to the original submission)

Sat 20 Feb 2016 05:29:58 AM UTC, comment #9: 

I fixed bug #33523 which also solves the problem reported in this bug's summary.  Closing report.

Rik <rik5>
Project Administrator
Thu 21 Jan 2016 07:19:05 AM UTC, comment #8: 

Thanks Markus.

For many of those functions, the correct answer is not simply the input.  For example, the interquartile range (iqr) is zero if the input is finite, or NaN otherwise.

I'll prepare a patch based on jwe's template in bug #33523.

Lachlan Andrew <lachlan>
Sun 21 Sep 2014 05:54:27 AM UTC, comment #7: 

I've reviewed and reworked sevaral statistic functions (center, iqr, kurtosis, mean, meansq, median, mode) and attached a patch. 

There are some functions left with exactly the same if clause. But I even don't know what they are exactly doing, so it's not easy to update the error and assert parts. They are still untouched. This are the files with their issueing line.

    histc.m:77: if (!(isscalar (dim) && dim == fix (dim))
    moment.m:164: if (!(isscalar (dim) && dim == fix (dim)) ||
    prctile.m:65: if (!(isscalar (dim) && dim == fix (dim))
    quantile.m:131: if (!(isscalar (dim) && dim == fix (dim))
    ranks.m:50: if (!(isscalar (dim) && dim == fix (dim))
    run_count.m:53: if (!(isscalar (dim) && dim == fix (dim))
    skewness.m:105: if (!(isscalar (dim) && dim == fix (dim)) || !(1 <= dim && dim <= nd))
    statistics.m:51: if (!(isscalar (dim) && dim == fix (dim))
    std.m:87: if (!(isscalar (dim) && dim == fix (dim))
    var.m:87: if (!(isscalar (dim) && dim == fix (dim))
    zscore.m:66: if (!(isscalar (dim) && dim == fix (dim))

(file #32152)

Markus Bergholz <markuman>
Sun 14 Sep 2014 02:33:52 PM UTC, comment #6: 

I added a file to Attachments.

Unfortunately statistics functions in matlab return not only input array if dimmension is greater than size of array.
For example: function iqr returns zeros array.

I'll try to find and fix some functions.

Dmitrey <lamazavr>
Tue 09 Sep 2014 05:10:51 PM UTC, comment #5: 

Thanks for your patch. Please post patches or changesets as attachments to the tracker, otherwise savannah can mangle the meaningful content of your patch.

Also note as I said in comment #1, it would be really nice to fix mean along with other statistics functions that this change also applies to. We try to use consistent idioms and syntax, so it should be easy to find all instances of this dimension error checking in Octave, and check the Matlab documentation to see which functions should also be fixed the same way.

Mike Miller <mtmiller>
Project Administrator
Tue 09 Sep 2014 01:27:22 PM UTC, comment #4: 

Hi, hear is diff file to fix this:
# HG changeset patch
# User Dmitrey Salnikov <mr.dimas@meta.ua>
# Date 1410269118 -10800
#      Tue Sep 09 16:25:18 2014 +0300
# Node ID c40955104bdede20e8d3b31cb99e1273c9e63fd5
# Parent  4630a18757b319688ce52d06917a49063ee3721f
mean fix

diff -r 4630a18757b3 -r c40955104bde scripts/statistics/base/mean.m
--- a/scripts/statistics/base/mean.m Tue Aug 26 15:48:02 2014 -0700
+++ b/scripts/statistics/base/mean.m Tue Sep 09 16:25:18 2014 +0300
@@ -103,8 +103,13 @@
     (dim = find (sz > 1, 1)) || (dim = 1);
   else
     if (!(isscalar (dim) && dim == fix (dim))
-      || !(1 <= dim && dim <= nd))
-      error ("mean: DIM must be an integer and a valid dimension");
+      || !(1 <= dim))
+      error ("mean: DIM must be an integer and a valid dimension @@ lamazavr");
+    endif
+   
+    if (!(dim <= nd))
+        y = x;
+        return;
     endif
   endif

@@ -155,4 +160,3 @@
 %!error mean (1, 0)
 %!error mean (1, 3)
 %!error mean (1, "b")
-

Dmitrey <lamazavr>
Mon 08 Sep 2014 12:34:07 AM UTC, comment #3: 

If you want to contribute to Octave, please read the manual section about contributing, in particular:

https://www.gnu.org/software/octave/doc/interpreter/Basics-of-Generating-a-Changeset.html

Mike Miller <mtmiller>
Project Administrator
Sun 07 Sep 2014 10:19:33 AM UTC, comment #2: 

Hi, matlab simply returns an input array if dimension is greater than it has.

I want to fix this in code. How can I commit code to repository ?

Dmitrey <lamazavr>
Thu 21 Aug 2014 02:58:36 AM UTC, comment #1: 

Thanks for your bug report. Do you have access to Matlab to demonstrate what the expected behavior should be?

According to my reading of the Matlab documentation for mean, it says "mean returns A if dim is greater than ndims(A)". And referring to the help for ndims, trailing singleton dimensions are dropped. Based on that, I would expect the same behavior that you suggest. It would be good to confirm the actual Matlab behavior.

It would also be good to check which other statistical functions that take a dimension argument this would apply to.

Mike Miller <mtmiller>
Project Administrator
Thu 21 Aug 2014 02:41:34 AM UTC, original submission:  

Dear Octave developers,

Thanks a million for the fantastic software. I just ran across the following error:

>> a = rand(2,2,1);
>> mean(a,3);

error: mean: DIM must be an integer and a valid dimension
error: called from:
error:   /usr/share/octave/3.8.1/m/statistics/base/mean.m at line 107, column 7

What happens is that the trailing singleton dimension is removed, and the variable 'a' has only two dimensions, so the error message is in essence correct.

However, this is causing trouble in the code I am currently working on: in my code, in general the third dimension is not a singleton, and the code works fine. But if the third dimension happens to be a singleton (in my case, it codes for the number of parameters I am testing in a simulation), I get an error. I would find it logical for mean(a,3) to return a, instead of an error. Actually, this behaviour would make sense for mean(a,n), with n greater than the number of dimensions of a.

What do you think ? Am I missing a potential issue with the suggested behaviour ?

Best,

Pierre

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #32152:  43038-1-tip.patch added by markuman (8KiB - text/x-patch)
file #32095:  mean.diff added by lamazavr (811B - text/x-patch)

 

Digest:
   bug dependencies.

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by lachlan (Posted a comment)
  • -email is unavailable- added by markuman (Updated the item)
  • -email is unavailable- added by lamazavr (Posted a comment)
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

     

     

    Follow 9 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-02-20 rik5 StatusDuplicate => Fixed
        Open/ClosedOpen => Closed
    2016-02-08 lachlan StatusNeed Info => Duplicate
        Release3.8.1 => dev
    2014-12-27 rik5 Dependencies- => Depends on bugs #33523
    2014-09-21 markuman Attached File- => Added 43038-1-tip.patch, #32152
    2014-09-14 lamazavr Attached File- => Added mean.diff, #32095
    2014-08-21 mtmiller Item GroupFeature Request => Matlab Compatibility
        StatusNone => Need Info

    Back to the top


    Powered by Savane 3.5