Let's say I have a 3D numpy matrix M
with shape (c, b, a)
. I want to shuffle a specific cell (x, y)
in M
along the z-axis --- that is, I want to shuffle the array [M[z][y][x] for z in range(c)]
and assign it back (somehow, I have no idea how that would work --- maybe with M[:, y, x]
?)
I can think of one way to do this: Flatten each 2D matrix, so M1
is a 2D matrix; transpose M1
to M2
, shuffle M2[x+y*a]
, transpose M2
back to M1
, then reform the 2D matrices.
However, this is clearly clunky. Is there a cleaner way to do this?
I found a solution: np.random.shuffle(M[:, y, x])
. Note that this modifies M
(you can make a copy by first executing M1=copy.deecopy(M)
before shuffling).