3

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.

Coolcrab
  • 2,655
  • 9
  • 39
  • 59
  • Just so you know `RAdeccircle` is already squared you don't need to square it again in the last statement. (r^2 = x^2 + y^2) – NendoTaka Jun 08 '15 at 16:10
  • possible duplicate of [How to apply a disc shaped mask to a numpy array?](http://stackoverflow.com/questions/8647024/how-to-apply-a-disc-shaped-mask-to-a-numpy-array) – stellasia Jun 08 '15 at 16:12

1 Answers1

5

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),)
ZdaR
  • 22,343
  • 7
  • 66
  • 87