Calculate Manhattan distances between these pairs of points (total 10*10 = 100
pairs inlcuding self pairs).This should give you a 10*10 2-D array where each value
corresponds to the distance between a pair
'''
Calculate Manhattan distances between these pairs of points (total 10*10 = 100
pairs inlcuding self pairs).This should give you a 10*10 2-D array where each value
corresponds to the distance between a pair
'''
def manhattan(a, b):
return sum(abs(val1-val2) for val1, val2 in zip(a,b))
ROW=10
COL=10
Points=[]
Dist=[]
print("Points Array:")
for r in range(0,ROW):
t=[]
s = ""
for c in range(0,COL):
t.append(random.randint(0,100))
s = s + "{:03d}".format(t[c])+" "
print(s)
Points.append(t)
Dist=[]
print("\nManhattan Distance Matrix:")
for r in range(0,ROW):
t=Points[r]
s = ""
k=[]
for c in range(0,ROW):
k.append(manhattan(t, Points[c]))
s = s + "{:03d}".format(k[c])+" "
print(s)
Comments
Leave a comment