I have these arrays:
A=np.array([160.592625, 161.616830, 161.672708, 163.544365, 163.745786, 164.260333, 164.277012]) # points on x-axis
B=np.array([53.090028, 54.829213, 54.573222, 47.244701, 52.033966, 48.613694, 53.425587]) # points on y-axis
I create the arrays of (x,y) coordinates:
coord = np.array([A, B])
I have another array of coordinates:
C=np.array([160.514, 161.67894, 161.68438, 160.59858, 161.55013, 161.61683, 161.55903, 161.67383, 161.70316, 161.63421 ])
D=np.array([53.068106, 54.552493,53.171848, 54.907098, 54.852462, 54.829213, 54.916358, 54.801067, 54.65673, 54.599929])
and a column of distances:
z=np.array([0.0452326, 0.903051, 0.126823, 0.101169, 0.000164209, 0.127296, 0.303595, 0.144146, 0.142063, 0.144751])
Now I want to create a zipped array of coordinates, so:
coord1=np.array([C,D])
The aim is the following: search where points in coord are in coord1 and then extract the corresponding distance from z. This is my code:
delta = 0.09
for i in range(coord.shape[1]):
for j in range(coord1.shape[1]):
if (np.all(coord[:,i] >= coord1[:,j]-delta)) and (np.all(coord[:,i] <= coord1[:,j]+delta)):
print coord[:,i], i, coord1[:,j], z[j], j
The output is the following:
[ 160.592625 53.090028] 0 [ 160.514 53.068106] 0.000164209 0
[ 161.61683 54.829213] 1 [ 161.55013 54.852462] 0.303595 4
[ 161.61683 54.829213] 1 [ 161.61683 54.829213] 0.144146 5
[ 161.61683 54.829213] 1 [ 161.55903 54.916358] 0.142063 6
[ 161.61683 54.829213] 1 [ 161.67383 54.801067] 0.903051 7
[ 161.672708 54.573222] 2 [ 161.67894 54.552493] 0.0452326 1
[ 161.672708 54.573222] 2 [ 161.70316 54.65673] 0.144751 8
[ 161.672708 54.573222] 2 [ 161.63421 54.599929] 0.101169 9
As you can see, I have multiple correspondences to 1 and 2. For these elements, I want to keep only the minimum z element. For example: among the 1s, I want to print only
[ 161.61683 54.829213] 1 [ 161.55013 54.852462] 0.303595 4
and among 2s only
[ 161.672708 54.573222] 2 [ 161.67894 54.552493] 0.0452326 1
I have no idea... Thanks in advance
I did not get much time to optimise this one but I hope this works well for your amount of data coz its not really huge -
You can run it directly and check if the finalResult
is giving you what you expected. Try to understand the algo though, its important.
import numpy as np
A = np.array([160.592625, 161.616830, 161.672708, 163.544365, 163.745786, 164.260333, 164.277012]) # points on x-axis
B = np.array([53.090028, 54.829213, 54.573222, 47.244701, 52.033966, 48.613694, 53.425587]) # points on y-axis
coord = np.array([A, B])
C = np.array(
[160.514, 161.67894, 161.68438, 160.59858, 161.55013, 161.61683, 161.55903, 161.67383, 161.70316, 161.63421])
D = np.array(
[53.068106, 54.552493, 53.171848, 54.907098, 54.852462, 54.829213, 54.916358, 54.801067, 54.65673, 54.599929])
z = np.array([0.0452326, 0.903051, 0.126823, 0.101169, 0.000164209, 0.127296, 0.303595, 0.144146, 0.142063, 0.144751])
coord1 = np.array([C, D])
delta = 0.09
# create a empty list and append whatever youre printing right now into it
mylst = list()
for i in range(coord.shape[1]):
for j in range(coord1.shape[1]):
if (np.all(coord[:, i] >= coord1[:, j] - delta)) and (np.all(coord[:, i] <= coord1[:, j] + delta)):
temp = str(coord[:, i]) + " , " + str(i) + " , " + str(coord1[:, j]) + " , " + str(z[j]) + " , " + str(j)
mylst.append(temp)
#see your list with all data
for each in mylst:
print(each)
print("--------")
#create a new list for final output where you want records with min(Z)
finalResult = list()
for each in mylst:
alpha = each
# check if this element is already in final result
alreadyDone = False
for eachFR in finalResult:
if str(alpha).split(",")[1] == str(eachFR).split(",")[1]:
alreadyDone = True
break
if alreadyDone:
continue
# if not, look for minimum value of Z
for ee in mylst:
if str(alpha).split(",")[1] == str(ee).split(",")[1]:
if str(alpha).split(",")[4] > str(ee).split(",")[4]:
alpha = ee
finalResult.append(alpha)
for each in finalResult:
print(each)