[Numpy-discussion] scan array to extract min-max values (with if condition)

Brett Olsen brett.olsen at gmail.com
Sun Sep 12 01:26:34 EDT 2010


On Sat, Sep 11, 2010 at 4:46 PM, Massimo Di Stefano
<massimodisasha at gmail.com> wrote:
> Thanks Pierre,
>
> i tried it and all works fine and fast.
>
> my apologize :-(
>
> i used a wrong "if" statment to represent my needs
>
> if mydata[i,0] < E or mydata[i,0] > W or mydata[i,1] < N or mydata[i,1] > S :
>
> ^^ totally wrong for my needs^^
>
>
> this "if"  instead :
>
> if W < mydata[i,0] < E and S < mydata[i,1] < N:
>
> should reflect your example :
>
> yselect = (data[:,1] <= N) & (data[:,1] >= S)
> xselect = (data[:,0] <= E) & (data[:,0] >= W)
> selected_data = data[xselect & yselect]
>
>
> a question, how to code a masked array,
> as in the Brett's code, to reflect the new (right) if statment ?

Just replace the lines

      mask |= mydata[:,0] < E
      mask |= mydata[:,0] > W
      mask |= mydata[:,1] < N
      mask |= mydata[:,1] > S

with

      mask &= mydata[:,0] < E
      mask &= mydata[:,0] > W
      mask &= mydata[:,1] < N
      mask &= mydata[:,1] > S

Sorry, I wasn't paying attention to what you were actually trying to
do and just duplicated the function of the code you supplied.

There's a good primer on how to index with boolean arrays at
http://www.scipy.org/Tentative_NumPy_Tutorial#head-d55e594d46b4f347c20efe1b4c65c92779f06268
that will explain why this works.

Brett



More information about the NumPy-Discussion mailing list