Thread Subject: operate over arrays with nans

Subject: operate over arrays with nans

From: Gautam Sethi

Date: 27 Dec, 2007 17:17:10

Message: 1 of 3

dear all:
suppose i have an array X = [nan nan nan; nan 2 4]

for each column of X, i wish to find the mean of its non-nan elements.
in this case, i want the output to be [nan 3]. is there an easy and
efficient way to do this?

mean(X(~isnan(X))) yields 3, but i want the column by column mean.

gautam.

Subject: operate over arrays with nans

From: Huy

Date: 27 Dec, 2007 17:37:12

Message: 2 of 3

Gautam Sethi <gautamsethi@gmail.com> wrote in message <5dd47ef1-
3946-4565-b223-6f1bdc1f1131@i3g2000hsf.googlegroups.com>...
> dear all:
> suppose i have an array X = [nan nan nan; nan 2 4]
>
> for each column of X, i wish to find the mean of its non-nan
elements.
> in this case, i want the output to be [nan 3]. is there an
easy and
> efficient way to do this?
>
> mean(X(~isnan(X))) yields 3, but i want the column by column
mean.
>
> gautam.


"column by column" or "row by row" ?

X(~isnan(X)): collect and generate a "NEW VECTOR" which
contains finite elements of X

So, mean(X(~isnan(X))) returns scalar result.

Anh Huy Phan
RIKEN - BSI

Subject: operate over arrays with nans

From: Jos

Date: 27 Dec, 2007 18:06:43

Message: 3 of 3

Gautam Sethi <gautamsethi@gmail.com> wrote in message
<5dd47ef1-3946-4565-b223-
6f1bdc1f1131@i3g2000hsf.googlegroups.com>...
> dear all:
> suppose i have an array X = [nan nan nan; nan 2 4]
>
> for each column of X, i wish to find the mean of its non-
nan elements.
> in this case, i want the output to be [nan 3]. is there
an easy and
> efficient way to do this?
>
> mean(X(~isnan(X))) yields 3, but i want the column by
column mean.
>
> gautam.

There's NANMEAN in the Stats Toolbox, or you can use this
approach:

q = ~isnan(X) ; % define non-NaNs
Y = X ; % operate on a copy
Y(~q) = 0 ;
M = sum(Y) ./ sum(q) ;

hth
Jos

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com