Reputation: 183
I have following 2D array:
MyData = array([x = [ 82, 210, 203, 234, 135, 92, 176, 146, 246, 35, 257, 227, 258,
132, 31, 160, 269, 24, 248, 274, 281, 279, 71, 21, 188, 163,
243],
y = [ 15, 16, 18, 18, 19, 21, 23, 29, 35, 47, 50, 53, 60,
64, 67, 69, 77, 88, 89, 91, 105, 115, 138, 175, 178, 205,
207]], dtype=int64)
I want to remove all the x and y pairs that are in specific Euclidean distance from each other.
For example, here, (210,16) and (203,18) have distance less than 10 and both should be removed.
However, before doing so, I need all the distances first (which is easy), and then I have to remove them.
So, I have created this matrix of distances:
distance = np.zeros((27,27))
for i in range (0 , 27):
for j in range (0 , 27):
dist= np.linalg.norm(MyData[:,i] - MyData[:,j])
distance[i,j] = dist
then using following conditions, i have found my indexes:
indx = (np.where((distance > 0) & (distance <= 10)))[0]
indy = (np.where((distance > 0) & (distance <= 10)))[1]
Now, I am not sure, how to filter 'MyData' using indexes I got from indx and indy.
Upvotes: 1
Views: 1083
Reputation: 5036
A solution with numpy
First prepare the data
import numpy as np
x = np.array([[ 82, 210, 203, 234, 135, 92, 176, 146, 246, 35, 257, 227, 258,
132, 31, 160, 269, 24, 248, 274, 281, 279, 71, 21, 188, 163,
243],
[ 15, 16, 18, 18, 19, 21, 23, 29, 35, 47, 50, 53, 60,
64, 67, 69, 77, 88, 89, 91, 105, 115, 138, 175, 178, 205,
207]]).T
Compute the distances for all pairs of points
a,b = np.tril_indices(27, -1)
diss = np.linalg.norm(x[b] - x[a], axis=1)
Find the points with distance smaller than a threshold
distance = 10
near = x[np.unique(np.concatenate([b[diss < distance], a[diss < distance]]))]
Then we can plot the points
import matplotlib.pyplot as plt
plt.scatter(x[:,0], x[:,1])
plt.scatter(near[:,0], near[:,1]);
To remove the points
remove = np.delete(x,np.unique(np.concatenate([b[diss < distance], a[diss < distance]])), axis=0)
plt.scatter(remove[:,0], remove[:,1]);
Upvotes: 3