I have a 4D Numpy array of shape (15, 2, 320, 320). In other words, each element of the [320 x 320] matrix is a matrix of size [15 x 2]. Now, I would like to compute the dot product for each element of the [320x320] matrix, then extract the diagonal array.
Currently, I am using 2 "for" loops, and the code works well (see the snippet). However, the calculation speed is too slow (when I process a large data). Anyone can show me how to vectorize the computation without the loops.
A = np.random.rand(15, 2, 320, 320)
B = np.zeros((2, 320, 320)) # store the final results
for row in range (320):
for col in range (320):
C = np.dot(A[:, :, row, col].T, A[:, :, row, col]) # the shape of C is [2 x 2]
C = np.diag(C)
B[:, row, col] = C
Any help or suggestions would be greatly appreciated!
(15,2,320,320)
or(320,320,15,2)
– fountainhead