Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
nansum vs nanmean

Subject: nansum vs nanmean

From: Young

Date: 27 Nov, 2012 14:51:08

Message: 1 of 5

I found nanmean and nansum differ in treating NaN. Please see below. Could you explain why nansum([NaN NaN NaN]') equals zero, not NaN?

>> nanmean([NaN NaN NaN]')

ans =

   NaN

>> nansum([NaN NaN NaN]')

ans =

     0

Subject: nansum vs nanmean

From: dpb

Date: 27 Nov, 2012 15:00:18

Message: 2 of 5

On 11/27/2012 8:51 AM, Young wrote:
> I found nanmean and nansum differ in treating NaN. Please see below.
> Could you explain why nansum([NaN NaN NaN]') equals zero, not NaN?
...

Behavior as documented...

doc nanmean

--

Subject: nansum vs nanmean

From: dpb

Date: 27 Nov, 2012 15:16:15

Message: 3 of 5

On 11/27/2012 9:00 AM, dpb wrote:
> On 11/27/2012 8:51 AM, Young wrote:
>> I found nanmean and nansum differ in treating NaN. Please see below.
>> Could you explain why nansum([NaN NaN NaN]') equals zero, not NaN?
> ...
>
> Behavior as documented...
>
> doc nanmean

While it is as documented, one _might_ wonder if nansum(NaN) ought not
to return [] rather than 0. But, of course, it is consistent w/
long-term behavior of Matlab in that sum([])=0 as well. Simply means
one can't tell by the result of the function that the input was empty
but if important to know need isempty() first.

Probably a behavior that can't change for compatibility reasons...

--

Subject: nansum vs nanmean

From: Peter Perkins

Date: 27 Nov, 2012 22:02:27

Message: 4 of 5

All true, but the original post was about why nansum([NaN NaN NaN]') is
zero while nanmean([NaN NaN NaN]') is NaN. The answer is, "because 0/0
equals NaN". The zero in the numerator is from nansum, the one in the
denominator is from having zero things to sum up.


On 11/27/2012 10:16 AM, dpb wrote:
> On 11/27/2012 9:00 AM, dpb wrote:
>> On 11/27/2012 8:51 AM, Young wrote:
>>> I found nanmean and nansum differ in treating NaN. Please see below.
>>> Could you explain why nansum([NaN NaN NaN]') equals zero, not NaN?
>> ...
>>
>> Behavior as documented...
>>
>> doc nanmean
>
> While it is as documented, one _might_ wonder if nansum(NaN) ought not
> to return [] rather than 0. But, of course, it is consistent w/
> long-term behavior of Matlab in that sum([])=0 as well. Simply means
> one can't tell by the result of the function that the input was empty
> but if important to know need isempty() first.
>
> Probably a behavior that can't change for compatibility reasons...
>
> --

Subject: nansum vs nanmean

From: dpb

Date: 27 Nov, 2012 23:09:35

Message: 5 of 5

On 11/27/2012 4:02 PM, Peter Perkins wrote:
> All true, but the original post was about why nansum([NaN NaN NaN]') is
> zero while nanmean([NaN NaN NaN]') is NaN. The answer is, "because 0/0
> equals NaN". The zero in the numerator is from nansum, the one in the
> denominator is from having zero things to sum up.
...

Which is why I pointed OP to the doc on nanmean on the first response;
that's where the NaN for a result is pointed out...nansum() doesn't
mention it (and, btw, each other aren't in the links in the doc pages,
either).

But, the result is actually []/[] :)

The followup was simply a comment on inconsistency of returning 0 as a
"sum" of empty...I'd submit it's also inconsistent to return NaN for the
mean() functions() on the basis of dividing by zero where that's so
because the input vector is empty, but it's closer than the other. :)

Again, it's too late to change (and not positive it would be better, but
in some ways it would be more consistent).

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us