0

Currently, I am using the loops to do the task below. It is quite similar to my previous question: Dot product two 4D Numpy array

Just wondering if anyone can help me to vectorize the computation without the loops.

A1 = np.random.rand(2, 320, 320)
A2 = np.random.rand(15, 2, 320, 320)
B   = np.zeros((2, 320, 320))

for row in range (320):
    for col in range (320):
        C = np.diag(A1[:, row, col]) - (np.dot(A2[:, :, row, col].T, A2[:, :, row, col]) / 15) # the shape of array C is (2, 2)
        C = np.diag(C)                
        B[:, row, col] = C

Any help or suggestions would be greatly appreciated!

2
  • Could you highlight what's new (relative to the previous)? Commented Jan 15, 2021 at 4:09
  • apparently, just the new A1 from which the previous output needs to be subtracted. I am not sure if it qualifies as a duplicate, almost does. Commented Jan 15, 2021 at 4:11

1 Answer 1

1

Similar to the last solution -

output = A1-(A2*A2).sum(0)/15
np.allclose(output,B)
True
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.