comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Mean = NaN if NaN present
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Mean = NaN if NaN present [message #91766] Mon, 24 August 2015 16:56 Go to next message
laura.hike is currently offline  laura.hike
Messages: 87
Registered: September 2013
Member
Hi,

I (obviously) using data with NaNs filling in for bad data. I would like to take the mean of a subset of the data and have it fail (return NaN) if any NaNs are included in the subarray. If I use

a = mean(subarray, /NaN)

this only eliminates the NaNs from the computation, meaning that a mean would be returned even if there was only one good value in the subarray. Is there any way to do this besides incorporating an IF statement before the computation, such as

if (total(finite(subarray)) eq n_elements(subarray)) then a = mean(subarray) else a = !Values.F_NAN

which is not only convoluted but may be a nuisance to implement when indices are used to define the subarray?

Thanks,

Larry
Mean = NaN if NaN present [message #91770 is a reply to message #91766] Tue, 25 August 2015 01:11 Go to previous messageGo to next message
Helder Marchetto is currently offline  Helder Marchetto
Messages: 520
Registered: November 2011
Senior Member
How about defining a function to do that?

function mn, subarray
if (total(finite(subarray)) eq n_elements(subarray)) then return, mean(subarray) else return, !Values.F_NAN
end

Cheers,
Helder
Re: Mean = NaN if NaN present [message #91778 is a reply to message #91766] Tue, 25 August 2015 07:08 Go to previous messageGo to next message
Jeremy Bailin is currently offline  Jeremy Bailin
Messages: 618
Registered: April 2008
Senior Member
On Monday, August 24, 2015 at 6:56:04 PM UTC-5, LMH wrote:
> Hi,
>
> I (obviously) using data with NaNs filling in for bad data. I would like to take the mean of a subset of the data and have it fail (return NaN) if any NaNs are included in the subarray. If I use
>
> a = mean(subarray, /NaN)
>
> this only eliminates the NaNs from the computation, meaning that a mean would be returned even if there was only one good value in the subarray. Is there any way to do this besides incorporating an IF statement before the computation, such as
>
> if (total(finite(subarray)) eq n_elements(subarray)) then a = mean(subarray) else a = !Values.F_NAN
>
> which is not only convoluted but may be a nuisance to implement when indices are used to define the subarray?
>
> Thanks,
>
> Larry

Don't you just want MEAN without the /NAN flag?

-Jeremy.
Re: Mean = NaN if NaN present [message #91781 is a reply to message #91778] Tue, 25 August 2015 14:45 Go to previous message
laura.hike is currently offline  laura.hike
Messages: 87
Registered: September 2013
Member
On Tuesday, August 25, 2015 at 7:08:41 AM UTC-7, Jeremy Bailin wrote:

>
> Don't you just want MEAN without the /NAN flag?
>
> -Jeremy.

Doh! I've been controlling for NaNs for too long! Thanks, Jeremy.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Unable to allocate memory: to make an array. Not enough space
Next Topic: how to prevent NaNs

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 11:24:56 PDT 2025

Total time taken to generate the page: 0.11283 seconds