How to rearrange data in matrix rows

3 次查看(过去 30 天)
Hi,
I have a large matrix (14484x10) 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!

采纳的回答

Stephen23
Stephen23 2016-1-27
编辑:Stephen23 2016-1-27
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by