Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python two arrays, get all points within radius

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.

like image 493
Coolcrab Avatar asked Apr 20 '26 05:04

Coolcrab


1 Answers

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),)
like image 103
ZdaR Avatar answered Apr 22 '26 20:04

ZdaR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!