Python two arrays, get all points in radius

I have two arrays, let's say x and y, that contain several thousand data points. Plotting a scatterplot gives an excellent idea of ​​them. Now, I would like to select all points within a specific radius. For example, r = 10

I've tried this but it doesn't work as it is not a grid.

x = [1,2,4,5,7,8,....]
y = [-1,4,8,-1,11,17,....]
RAdeccircle = x**2+y**2
r = 10

regstars = np.where(RAdeccircle < r**2)

      

This is not the same as the nxn array, and RAdeccircle = x**2+y**2

doesn't seem to work as it doesn't try to permutate.

+3


source to share


1 answer


You can only execute **

on a numpy array, but in your case you are using lists and using **

on a list returns an error, so you first need to convert the list to a numpy array usingnp.array()



import numpy as np


x = np.array([1,2,4,5,7,8])
y = np.array([-1,4,8,-1,11,17])
RAdeccircle = x**2+y**2

print RAdeccircle

r = 10

regstars = np.where(RAdeccircle < r**2)
print regstars

>>> [  2  20  80  26 170 353]
>>> (array([0, 1, 2, 3], dtype=int64),)

      

+5


source







All Articles