Reputation: 262
I have a (n,n,d,d) shaped tensor A.
How can I efficiently reshape it into an (ndim, ndim) block matrix B, such that A[i,j,k,l] = B[id+k, jd+l]? In other words, I want to tile the matrix B block by block in the last two dimensions of A.
Upvotes: 0
Views: 157
Reputation:
You need to transpose the array first so that they are adjacent to each other. You can then reshape to desired shape to get what you asked for
for example:
import numpy as np
n1 = n2 = 100
d1 = d2 = 4
A = np.random.rand(n1,n2,d1,d2)
B = A.transpose(0,2,1,3) ## transpose so that that they are adjacent (exactly what you wrote)
# B now has a shape (n1,d1,n2,d2)
C = B.reshape(n1*d1, n2*d2) ## your required array
Upvotes: 1