Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to calculate mean2 with matrices containing NaNs

333 views
Skip to first unread message

Evan

unread,
Mar 20, 2006, 9:35:29 PM3/20/06
to
I simply just want to calculate the mean of a m by n matrix (usually
you would just use mean2). However the matrices conatin NaNs, there
is a nanmean for 1D vectors but no nanmean2. How can i get around
this?

Thank you very much in advance for any help

Evan.

us

unread,
Mar 20, 2006, 10:45:06 PM3/20/06
to
Evan:
<SNIP slight misunderstanding...

> there is a nanmean for 1D vectors but no nanmean2...

one of the suggestions

carefully read

help nanmean;

again and pay attention to the sentence starting with
...For matrix input, M is a...

us

Charlotte Chaze

unread,
Jan 13, 2017, 3:41:07 PM1/13/17
to
"Evan" wrote in message <ef2d0...@webx.raydaftYaTP>...
There's a surprisingly easy way! Since nanmean takes the mean of each column and then puts those means into an array, do nanmean multiple times until you have the desired number of values.

For example,
avg = nanmean(nanmean(nanmean(my_array)));

Charlotte Chaze

unread,
Jan 13, 2017, 3:43:07 PM1/13/17
to
"Evan" wrote in message <ef2d0...@webx.raydaftYaTP>...

Bruno Luong

unread,
Jan 13, 2017, 4:10:09 PM1/13/17
to
"Charlotte Chaze" <cch...@udel.edu> wrote in message <o5be4n$ahb$1...@newscl01ah.mathworks.com>...

> There's a surprisingly easy way! Since nanmean takes the mean of each column and then puts those means into an array, do nanmean multiple times until you have the desired number of values.

That is not a wise advise. Run this to see why you have to be careful about take the average of the average where the number of invalid elements are dispersed.

A=nan(100,100);
A(1,:)=1;
A(:,1)=1e6;

% Replace with nanmean with you have stats tbx

mean(mean(A,'omitnan'),'omitnan')
mean(mean(A','omitnan'),'omitnan')
mean(A(:),'omitnan')
0 new messages