Calculations on vectors containing NaN

  • 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 (15) 7/22/2012 6:58:13 PM

"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 aclassyguy_with_a_k_not_a_c (948) 7/22/2012 7:06:24 PM


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 (15) 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 (6655) 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 (6348) 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 (6655) 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 (6348) 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 (6655) 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 (52) 7/23/2012 1:07:25 AM

8 Replies
39 Views

(page loaded in 0.117 seconds)

Similiar Articles:






7/23/2012 12:18:14 PM


Reply: