[Numpy-discussion] scan array to extract min-max values (with if condition)
Sun Sep 12 00:26:34 CDT 2010
On Sat, Sep 11, 2010 at 4:46 PM, Massimo Di Stefano
> 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
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
that will explain why this works.
More information about the NumPy-Discussion