[SciPy-User] finding max value in a vector which contains NaN's

Benjamin Root ben.root at ou.edu
Wed May 12 10:48:35 EDT 2010


FYI, if you are coming from another language like Matlab, you may have been
used to using NaNs to indicate bad values and such (not that there is
anything wrong with that!).  However, Numpy offers an interesting way to
deal with bad values in arrays called "Masked Arrays".

>>> import numpy as np
>>> x = np.array([2, 1, 3, np.nan, 5, 2, 3, np.nan])

>>> np.max(x)
nan

>>> m = np.ma.masked_array(x, np.isnan(x))
>>> np.max(m)
5.0

Ben Root


On Tue, May 11, 2010 at 11:24 PM, Pierre GM <pgmdevlist at gmail.com> wrote:

> On May 11, 2010, at 11:47 PM, Oz Nahum wrote:
> > Hi All,
> > I have a code that needs to find a max value in a vector, which has also
> NaN.
> > using max(cr), I get answer: nan, even though, the largest value is
> 15.1879....
> >
> > Anyone has an idea how to avoid this problem ? I don't want to make a
> > loop to kick out all the NaN values, although that would be a
> > solution...
>
>
> Use `nanmax` (a numpy function).
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20100512/eb55cd5c/attachment.html>


More information about the SciPy-User mailing list