bugGNU Octave - Bugs: bug #51437, mean(zeros(1,0)) does not return...

 
 

bug #51437: mean(zeros(1,0)) does not return NaN for Matlab compatibility

Submitter:  None
Submitted:  Tue 11 Jul 2017 07:43:34 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Wont Fix Assigned to:  None
Originator Name:  Reimund Renner Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * 4.2.1
Operating System:  * Any Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Tue 25 Jul 2017 07:59:13 AM UTC, comment #9: 

I understand that NaN or empty array is returned depending on dimension.

Strange was directed to the case that
mean(zeros(1,0)) leads to dim 2 and
mean(zeros(2,0)) leads to dim 1.

This is not generic beahvior in my opinion.

Reimund Renner <rennere>
Mon 24 Jul 2017 11:28:06 PM UTC, comment #8: 

Other languages which are far stronger in statistics, such as R, return NaN for the case of empty matrices.  Sample R code,


x = matrix (c(1), nrow=3, ncol=0)
mean(x)


It seems more useful mathematically for Octave to return NaN in these cases rather than occasionally NaN and occasionally a dimensioned empty matrix, even if there is a rule for when "occasionally" occurs.

Rik <rik5>
Group administrator
Mon 24 Jul 2017 10:34:21 PM UTC, comment #7: 

It's not strange behaviour. 'mean' with 1 input applies along the first non-singleton dimension. 'mean (zeros (1,0))' averages along the second dim and so gives NaN. 'mean (zeros (2,0))' averages along the first and so gives zeros(1,0). Try using dimension as a second input to 'mean'

Luis Mendo <lmendo>
Mon 17 Jul 2017 08:09:34 AM UTC, comment #6: 

It's a strange behavior:

mean (zeros (0,0)) = NaN
mean (zeros (1,0)) = NaN
mean (zeros (2,0)) = 1x0

Documentation of mean() only says about NaN:
If A is an empty 0-by-0 matrix, then mean(A) returns NaN

The same for std().

Reimund Renner <rennere>
Fri 14 Jul 2017 09:02:22 PM UTC, comment #5: 

Why, as a mathematical proposition, shouldn't mean() return NaN for all empty matrices?

I can't find any easy pattern to the results that you have from Matlab.  It seems to me that this may be an instance where Octave does not want to follow Matlab exactly since there seems to be quirks of implementation here, rather than reasonable mathematical results.

If you could, try these further tests in Matlab:


mean (zeros (0,0))
mean (zeros (1,0))
mean (zeros (2,0))



Rik <rik5>
Group administrator
Thu 13 Jul 2017 05:38:50 AM UTC, comment #4: 

Results are from Matlab 2016b.

Reimund Renner <rennere>
Thu 13 Jul 2017 05:36:38 AM UTC, comment #3: 

Here's a complete list up to dimesion three for mean(zeros(i,j,k), dim) and last for mean(zeros(i,j,k)) without dim:

i j k dim ret

  1. 0 0  1  1×0×0
  2. 0 1     1×0
  3. 1 0     1×1×0
  4. 1 1     NaN

1 0 0     1×0×0
1 0 1     1×0
1 1 0     1×1×0
1 1 1     0

i j k dim ret

  1. 0 0  2  0×1×0
  2. 0 1     0x1
  3. 1 0     0×1×0
  4. 1 1     0x1

1 0 0     1x1x0
1 0 1     NaN
1 1 0     1x1x0
1 1 1     0

i j k dim ret

  1. 0 0  3  []
  2. 0 1     []
  3. 1 0     0x1
  4. 1 1     0x1

1 0 0     1x0
1 0 1     1x0
1 1 0     NaN
1 1 1     0

i j k dim ret

  1. 0 0     1x0x0
  2. 0 1     NaN
  3. 1 0     1x1x0
  4. 1 1     NaN

1 0 0     1x1x0
1 0 1     NaN
1 1 0     NaN
1 1 1     0

Hope this is useful.

Reimund Renner <rennere>
Wed 12 Jul 2017 06:20:06 PM UTC, comment #2: 

Confirmed.  I'm pretty sure Matlab changed this relatively recently and that it used to return [].  It doesn't look that hard to fix, just a special test for empty inputs.  You can see that Matlab has changed because the Octave function supports a flag that allows you to specify arithmetic mean, geometric mean, or harmonic mean.  That used to be part of Matlab.

Rik <rik5>
Group administrator
Tue 11 Jul 2017 10:11:11 AM UTC, comment #1: 

The same for the std() method.

Reimund Renner <rennere>
Tue 11 Jul 2017 07:43:34 AM UTC, original submission:  

In Matlab mean of an empty input returns NaN, however,
Octave returns [](1x0) for "mean(zeros(1,0))".

Obviously it is the case if empty input size at dimension dim is not zero, e.g. also "mean(zeros(0,1), 2)" returns [](0x1).

Anonymous

 

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

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by lmendo (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by rennere (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-01-27 rik5 StatusConfirmed Wont Fix
        Open/ClosedOpen Closed
    2017-07-12 rik5 StatusNone Confirmed
        Operating SystemMicrosoft Windows Any
        Summarymean(zeros(1,0)) does not return NaN mean(zeros(1,0)) does not return NaN for Matlab compatibility

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code