I have a numpy array that is interleaved in a tricky way and I can't figure out an easy way to de-interleave it. Suppose the (84, 132) matrix is:
0 100 200 ...
1 101 201 ...
2 102 202 ...
...
83 183 283 ...
I want to take every fourth element from the first column, then every fourth element starting from the second row, then every fourth starting from the third row, then every fourth starting from the fourth row. (Yielding four new columns.) Then I want to repeat for the second column, and so forth. So the (21, 528) result I want is:
0 1 2 3 100 101 102 103 200 ...
4 5 6 7 104 105 106 107 204 ...
8 9 10 11 108 109 110 111 208 ...
...
80 81 82 83 180 181 182 183 283 ...
I can do this with a loop, converting the (84, 132) array a
to a (21, 528) array b
:
b = np.zeros(shape=(21, 132*4))
for y in range(0, 21):
for x in range(0, 132):
for s in range(0, 4):
b[y, x * 4 + s] = a[y * 4 + s, x]
Is there a nicer way to do this with numpy operations?
(Context: this is the physical arrangement of the microcode ROM in the 8086 processor and I'm trying to unshuffle the bits for analysis.)