jvnna jvnna - 1 year ago 67
Python Question

Filter one array according to data in another array

I have 2 arrays of random numbers, one array (X) representing x-coordinates and one array (Y) representing y-coordinates. I want to filter the X array such that I only keep the indices (i) of X where X[i]^2 + Y[i]^2 < 1.

I know how to filter with values in 1 array but since I need to use 2, I am not sure what to do. I am not allowed to use loops of any kind.

Answer Source

This will do:

X_filtered = X[X**2 + Y**2 < 1]

X**2 + Y**2 < 1 returns a boolean array and accessing X on this array returns the X only at the indices equal to True.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download