MATLAB: How to rearrange data in matrix rows

rearrangereshaperowwise

Hi,
I have a large matrix (14484×10) on this form:
[20031119 1 2 3 4 5 6 7 8 9;
10 11 12 13 14 15 16 17 18 19;
20 21 22 23 24 25 26 27 28 29;
30 31 NaN NaN NaN NaN NaN NaN NaN NaN;
20031120 1 2 3 4 5 6 7 8 9;
10 11 12 13 14 15 16 17 18 19;
20 21 22 23 24 25 26 27 28 29;
30 31 NaN NaN NaN NaN NaN NaN NaN NaN;
20031121 1 2 3 4 5 6 7 8 9;
...and so on]
I want to rearrange the rows so that the first 4 rows turn makes the first row in the new matrix, the next 4 rows makes the second row etc, so that the resulting matrix looks like this:
[20031119 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31;
20031120 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31;
...and so on]
(I don't need the NaNs, but it's of course OK to keep them and delete them later.)
How do I do this? I have tried to do it using reshape, but it seems to me that reshape works columnwise and not rowwise.
I would appreciate any help!

Best Answer

reshape, like pretty much all MATLAB operations, works columnwise. So you just need to transpose, reshape, transpose:
X = [20031119 1 2 3 4 5 6 7 8 9;
10 11 12 13 14 15 16 17 18 19;
20 21 22 23 24 25 26 27 28 29;
30 31 NaN NaN NaN NaN NaN NaN NaN NaN;
20031120 1 2 3 4 5 6 7 8 9;
10 11 12 13 14 15 16 17 18 19;
20 21 22 23 24 25 26 27 28 29;
30 31 NaN NaN NaN NaN NaN NaN NaN NaN];
out = reshape(X.',[],size(X,1)/4).';
creates this output matrix:
>> out
out =
Columns 1 through 9:
20031119 1 2 3 4 5 6 7 8
20031120 1 2 3 4 5 6 7 8
Columns 10 through 18:
9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17
Columns 19 through 27:
18 19 20 21 22 23 24 25 26
18 19 20 21 22 23 24 25 26
Columns 28 through 36:
27 28 29 30 31 NaN NaN NaN NaN
27 28 29 30 31 NaN NaN NaN NaN
Columns 37 through 40:
NaN NaN NaN NaN
NaN NaN NaN NaN