I'm trying to find a way to avoid a double for loop in python by using numpy, but I'm not sure if it's even possible.
I have two 3D matrices that I've flatten into two 2D matrices using numpy's flatten(), now I need to do a calculation with each row, to every row. Basically each row represents an image, and I'm doing a series of calculations to two vectors, and returning a scalar.
A [a, b, c, d] A' [a, b, c, d]
B [e, f, g, h] B' [e, f, g, h]
C [i, j, k, l] C' [i, j, k, l]
D [m, n, o, p] D' [m, n, o, p]
result
[AA' AB' AC' AD']
[BA' BB' BC' BD']
[CA' CB' CC' CD']
[DA' DB' DC' DD']
EDIT: Here's my double for loop
aMatrix = np.array([[5, 3, 2, 1, 4, 2],
[7, 0, 3, 5, 7, 9],
[9, 8, 0, 2, 4, 8],
[3, 5, 2, 0, 1, 9],
[7, 7, 4, 1, 7, 6],
[5, 9, 8, 9, 6, 1]])
find_distance_of_two_sets(aMatrix, aMatrix)
def find_distance_of_two_sets(aMatrix, bMatrix):
distance = np.zeros((6, 6))
i = 0
for a in aMatrix:
j = 0
for b in bMatrix:
distance[i][j] = euclidean_distance(a, b)
j += 1
i += 1
outputFile = open('distanceMatrix', 'wb')
np.save(outputFile, distance)
def euclidean_distance(a, b):
return np.sqrt(np.sum(np.square(np.subtract(a, b))))
and if you were to print the result it would be
[[ 0. 9.38083152 9.05538514 8.18535277 7. 11.87434209]
[ 9.38083152 0. 9.79795897 10.14889157 8.66025404 13.82027496]
[ 9.05538514 9.79795897 0. 7.93725393 5.91607978 13.52774926]
[ 8.18535277 10.14889157 7.93725393 0. 8.36660027 15.03329638]
[ 7. 8.66025404 5.91607978 8.36660027 0. 10.67707825]
[11.87434209 13.82027496 13.52774926 15.03329638 10.67707825 0. ]]