Python two arrays, get all points within radius

1.3k views Asked by At

I have two arrays, lets say x and y that contain a few thousand datapoints. Plotting a scatterplot gives a beautiful representation of them. Now I'd like to select all points within a certain radius. For example r=10

I tried this, but it does not work, as it's 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 an nxn array, and RAdeccircle = x**2+y**2 does not seem to work as it does not try all permutations.

1

There are 1 answers

0
ZdaR On

You can only perform ** 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 numpy array using np.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),)