[Numpy-discussion] Getting an array's indices when a given condition is true

Lane Brooks lbrooks at MIT.EDU
Sat Oct 18 23:00:42 EDT 2008


If you want the indexes, check out the np.where command, e.g.

idx = np.where(dat <= limit)

If you want the values, use:
val = dat[dat <= limit]

Lane


Michael wrote:
> Hi list, 
>
> been playing around with stride_tricks and find it terrifically
> productive; thankyou to everyone who has worked on this.  
>
> I need to filter some data, getting the indices of all entries which are
> less than or equal to 'limit'. How do i best go about that? 
>
> Can you enumerate an array using broadcasting?
>
> dat=np.array([  0.           1.61803399   3.23606798   4.85410197
> 6.47213595
>    8.09016994   9.70820393  11.32623792  12.94427191  14.5623059    1.
>    0.61803399   2.23606798   3.85410197   5.47213595   7.09016994
>    8.70820393  10.32623792  11.94427191  13.5623059    2.
> 0.38196601
>    1.23606798   2.85410197   4.47213595   6.09016994   7.70820393
>    9.32623792  10.94427191  12.5623059    3.           1.38196601
>    0.23606798   1.85410197   3.47213595   5.09016994   6.70820393
>    8.32623792   9.94427191  11.5623059    4.           2.38196601
>    0.76393202   0.85410197   2.47213595   4.09016994   5.70820393
>    7.32623792   8.94427191  10.5623059    5.           3.38196601
>    1.76393202   0.14589803   1.47213595   3.09016994   4.70820393
>    6.32623792   7.94427191   9.5623059    6.           4.38196601
>    2.76393202   1.14589803   0.47213595   2.09016994   3.70820393
>    5.32623792   6.94427191   8.5623059    7.           5.38196601
>    3.76393202   2.14589803   0.52786405   1.09016994   2.70820393
>    4.32623792   5.94427191   7.5623059    8.           6.38196601
>    4.76393202   3.14589803   1.52786405   0.09016994   1.70820393
>    3.32623792   4.94427191   6.5623059    9.           7.38196601
>    5.76393202   4.14589803   2.52786405   0.90983006   0.70820393
>    2.32623792   3.94427191   5.5623059 ])
>
> limit=1.30901699437
>
> for i in dat: 
>     if i <=limit: 
> 	print i 	
>
> Michael
>
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20081018/cefbc9ce/attachment.html>


More information about the NumPy-Discussion mailing list