jvnna jvnna - 1 month ago 11
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

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.