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

 Submitted by: None Submitted on: Tue 11 Jul 2017 07:43:34 AM UTC Category: Octave Function Severity: 3 - Normal Priority: 5 - Normal Item Group: Matlab Compatibility Status: Confirmed Assigned to: None Originator Name: Reimund Renner Originator Email: -unavailable- Open/Closed: Open Release: 4.2.1 Operating System: Any

## You are not logged in

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

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,

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>
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:

Rik <rik5>
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>
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 File(s):

Comment:

No files currently attached

Depends on the following items: None found

Items that depend on this one: None found

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

Do you think this task is very important?
This task has 0 encouragements so far.

Only project members can vote.

Please enter the title of George Orwell's famous dystopian book (it's a date):