How to rearrange data in matrix rows

조회 수: 3 (최근 30일)
SiljeH
SiljeH 2016년 1월 27일
댓글: SiljeH 2016년 1월 27일
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