COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Calculations on vectors containing NaN

• Email
• Follow

```Hi all,

I have N vectors of different lengths, and I want to calculate the mean of all the numeric values in each vector (so I want N means).

Matlab will return a NaN mean if I have NaNs in a vector, how do I get around this?

Many thanks,
IC
```
 0
Reply i.charvet (18) 7/22/2012 6:58:13 PM

See related articles to this posting

```"Ingrid " <i.charvet@ucl.ac.uk> wrote in message <juhig5\$isu\$1@newscl01ah.mathworks.com>...
> Hi all,
>
> I have N vectors of different lengths, and I want to calculate the mean of all the numeric values in each vector (so I want N means).
>
> Matlab will return a NaN mean if I have NaNs in a vector, how do I get around this?
>
> Many thanks,
> IC

doc nanmean

James Tursa
```
 0

```Brilliant thanks!

"James Tursa" wrote in message <juhivg\$ken\$1@newscl01ah.mathworks.com>...
> "Ingrid " <i.charvet@ucl.ac.uk> wrote in message <juhig5\$isu\$1@newscl01ah.mathworks.com>...
> > Hi all,
> >
> > I have N vectors of different lengths, and I want to calculate the mean of all the numeric values in each vector (so I want N means).
> >
> > Matlab will return a NaN mean if I have NaNs in a vector, how do I get around this?
> >
> > Many thanks,
> > IC
>
> doc nanmean
>
> James Tursa
```
 0
Reply i.charvet (18) 7/22/2012 7:16:28 PM

```On 7/22/2012 2:16 PM, Ingrid wrote:
> Brilliant thanks!
> "James Tursa" wrote in message <juhivg\$ken\$1@newscl01ah.mathworks.com>...
>> "Ingrid " <i.charvet@ucl.ac.uk> wrote in message
>> <juhig5\$isu\$1@newscl01ah.mathworks.com>...
>> > Hi all,
>> > > I have N vectors of different lengths, and I want to calculate the
>> mean of all the numeric values in each vector (so I want N means). > >
>> Matlab will return a NaN mean if I have NaNs in a vector, how do I get
>> around this?
>> > > Many thanks,
>> > IC
>>
>> doc nanmean

Or, in illustration of general principle if there isn't a
builtin/supplied function (using mean as example)

mean(~isnan(x))

--

```
 0
Reply none1568 (7455) 7/22/2012 7:20:13 PM

```dpb <none@non.net> wrote in message <juhjpa\$597\$1@speranza.aioe.org>...
>
> mean(~isnan(x))
>

mean(x(~isnan(x)))

But this can only work for vectors.

Bruno
```
 0
Reply b.luong5955 (6403) 7/22/2012 7:29:16 PM

```On 7/22/2012 2:29 PM, Bruno Luong wrote:
> dpb <none@non.net> wrote in message <juhjpa\$597\$1@speranza.aioe.org>...
>>
>> mean(~isnan(x))
>>
>
> mean(x(~isnan(x)))
>
> But this can only work for vectors.

That is true unless newer ML has a syntax to return results in cells,
maybe????

--

```
 0
Reply none1568 (7455) 7/22/2012 8:14:04 PM

```dpb <none@non.net> wrote in message <juhmua\$cbv\$1@speranza.aioe.org>...
>
> That is true unless newer ML has a syntax to return results in cells,
> maybe????
>

I'm not talking about CELL. The above command fails for

A=[nan 2;
1 2]

Bruno
```
 0
Reply b.luong5955 (6403) 7/22/2012 8:28:24 PM

```On 7/22/2012 3:28 PM, Bruno Luong wrote:
> dpb <none@non.net> wrote in message <juhmua\$cbv\$1@speranza.aioe.org>...
>>
>> That is true unless newer ML has a syntax to return results in cells,
>> maybe????
>>
>
> I'm not talking about CELL. The above command fails for
>
> A=[nan 2; 1 2]

I know/understand/grok; I was asking if there was a way in newer ML
syntax to return the previous _to_ a cell array instead so it
didn't/wouldn't fail.

--

```
 0
Reply none1568 (7455) 7/22/2012 8:48:43 PM

```On Sunday, July 22, 2012 1:58:13 PM UTC-5, Ingrid  wrote:
> Hi all,
>
> I have N vectors of different lengths, and I want to calculate the mean of all the numeric values in each vector (so I want N means).
>
> Matlab will return a NaN mean if I have NaNs in a vector, how do I get around this?
>
> Many thanks,
> IC

remove the not numbers (nans) first from vector to do the calcs after then will be correct numerical program
```
 0
Reply steve.nospamm (54) 7/23/2012 1:07:25 AM

8 Replies
53 Views

Similar Articles

12/10/2013 4:36:12 AM
[PageSpeed]