Skip to content Skip to sidebar Skip to footer

Reshape 4d Numpy Array To 2d Array While Preserving Array Locations

I have a 4 dimensional numpy array of shape (N, N, Q, Q). So given a row and column index (i, j), mat[i,j] is a QxQ matrix. I want to reshape this array to shape (N*Q, N*Q) such t

Solution 1:

Nevermind, I figured it out. np.swapaxes(1, 2) was the missing piece I needed.

The answer is just to do mat.swapaxes(1, 2).reshape(N*Q, N*Q).

Feel foolish for posting without attempting to figure it out myself for too long, but I'll leave it up so others can benefit from it.


Post a Comment for "Reshape 4d Numpy Array To 2d Array While Preserving Array Locations"