Home > Net >  How to remove a specific value from two lists if they exceed a given Euclidean distance?
How to remove a specific value from two lists if they exceed a given Euclidean distance?

Time:12-16

I have these two lists of data x & y. Now I want to remove the specific cartesian point (x,y) which has more than a specific euclidean distance(Example: 4). How to do that?

x = [92.5 , 92.75 ,92.75, 93. ,  93.,   93.25 ,93.25, 93.25,
 93.25 ,93.25 ,93.25 ,93.25 ,93. ,  92.5,  93.  , 93.  , 93.,  
 92.5 , 92.75, 93.  , 93.25, 93.25 ,93.5  ,93.5 , 93.5  ,93.5 , 
 91.5 , 92.5  ,92.5  ,92.5 , 92. ,  92.,   92.  , 91.75 ,91.5 , 
 91.5 , 91.25 ,91.  , 91.25 ,91.25, 91.25 ,91.]

y = [17.75, 17.75 ,18. ,  18. ,  18.  , 18.25, 18.25 ,18.5,
18.5 , 18.5 , 18.75 ,18.75 ,18.75, 24.75 ,19. ,  18.75, 18.75, 
 18.75 ,18.75 ,18.75 ,19.   ,19. ,  19.25, 19.5 , 19.5 , 19.75 ,
 24.25 ,24.75 ,24.75 ,24.75 ,24.25, 24.25 ,24.25 ,24.25 ,24.75, 
 24.25 ,23.75, 23.75 ,23.75 ,24.25 ,23.75 ,23.75]

This is what I did, but didn't work out.

for k in range(len(x)):
  x1 = x[k]
  y1 = y[k]
  x2 = x[k 1]
  y2 = y[k 1]

  distance = np.sqrt((x2-x1)**2 (y2-y1)**2)
  
  if distance>4:
    x.remove(k)
    y.remove(k) 

CodePudding user response:

this can solve your issue,

import numpy as np

newX, newY = [], []
for k in range(len(x) - 1):
    if np.sqrt((x[k   1] - x[k]) ** 2   (y[k   1] - y[k]) ** 2) <= 4:
        newX.append(x[k])
        newY.append(y[k])

newX:

[92.5, 92.75, 92.75, 93.0, 93.0, 93.25, 93.25, 93.25, 93.25, 93.25, 93.25, 93.25, 93.0, 93.0, 93.0, 92.5, 92.75, 93.0, 93.25, 93.25, 93.5, 93.5, 93.5, 91.5, 92.5, 92.5, 92.5, 92.0, 92.0, 92.0, 91.75, 91.5, 91.5, 91.25, 91.0, 91.25, 91.25, 91.25]

newX:

[17.75, 17.75, 18.0, 18.0, 18.0, 18.25, 18.25, 18.5, 18.5, 18.5, 18.75, 18.75, 19.0, 18.75, 18.75, 18.75, 18.75, 18.75, 19.0, 19.0, 19.25, 19.5, 19.5, 24.25, 24.75, 24.75, 24.75, 24.25, 24.25, 24.25, 24.25, 24.75, 24.25, 23.75, 23.75, 23.75, 24.25, 23.75]

CodePudding user response:

It won't be a good idea to change the list on which the loop is running. This code might help you

x = [92.5 , 92.75 ,92.75, 93. ,  93.,   93.25 ,93.25, 93.25, 93.25 ,93.25 ,93.25 ,93.25 ,93. ,  92.5,  93.  , 93.  , 93., 92.5 , 92.75, 93.  , 93.25, 93.25 ,93.5  ,93.5 , 93.5  ,93.5 , 91.5 , 92.5  ,92.5  ,92.5 , 92. ,  92.,   92.  , 91.75 ,91.5 , 91.5 , 91.25 ,91.  , 91.25 ,91.25, 91.25 ,91.]
y = [17.75, 17.75 ,18. ,  18. ,  18.  , 18.25, 18.25 ,18.5, 18.5 , 18.5 , 18.75 ,18.75 ,18.75, 24.75 ,19. ,  18.75, 18.75, 18.75 ,18.75 ,18.75 ,19.   ,19. ,  19.25, 19.5 , 19.5 , 19.75 , 24.25 ,24.75 ,24.75 ,24.75 ,24.25, 24.25 ,24.25 ,24.25 ,24.75, 24.25 ,23.75, 23.75 ,23.75 ,24.25 ,23.75 ,23.75]
x_tmp, y_tmp = [], []
for k in range(len(x)-1): # Till len(x) will give index out of range error
    x1, x2, y1, y2 = x[k], x[k 1], y[k], y[k 1]
    distance = np.sqrt((x2-x1)**2 (y2-y1)**2)
    if distance<=4:
        x_tmp.append(x[k])
        y_tmp.append(y[k])
print(x_tmp, y_tmp)

Also, where have you defined 'i'.

CodePudding user response:

Faster, concise solution:

dists = np.sqrt(np.diff(x)**2   np.diff(y)**2)
mask = np.nonzero(dists <= 4)[0]

new_x = x[mask]
new_y = y[mask]

For this you'd need to convert your lists to numpy arrays first: x = np.array(x).

This solution is equivalent to the others presented in this thread.

CodePudding user response:

You can zip x and y, along with one shifted lists of these two and iterate over this zipped object. For testing, instead of taking square root by using an outside library, square each number. This solution is probably the fastest one here including the numpy solution.

new_x, new_y = [], []
for x1,x2,y1,y2 in zip(x,x[1:],y,y[1:]):
    if (x2 - x1) *(x2 - x1)   (y2 - y1) *(y2 - y1) <= 4 *4:
        new_x.append(x1)
        new_y.append(y1)
  • Related