필터 지우기
필터 지우기

Reshape matrix by taking two consecutive rows every two rows

조회 수: 15 (최근 30일)
Pascal Weller
Pascal Weller 2021년 3월 23일
댓글: Pascal Weller 2021년 4월 7일
Hello everyone,
I have seen quite some questions here on the forum that are about matrix reshaping. However, for my particular problem I did not find an answer. So, here we are:
I have got a matrix that looks like
[1 1 1 1;
2 2 2 2;
3 3 3 3;
4 4 4 4;
5 5 5 5;
6 6 6 6;
7 7 7 7;
8 8 8 8]
Is it somehow possible to reshape it to
[1 1 1 1 3 3 3 3;
2 2 2 2 4 4 4 4;
5 5 5 5 7 7 7 7;
6 6 6 6 8 8 8 8]
with the built-in functions like reshape, permutate, flip,...?
There would always be the way with for loops but I thought it might be neater (and possibly faster) to do it with the built-in functions.
Thanks for your help.

채택된 답변

Adam Danz
Adam Danz 2021년 3월 23일
편집: Adam Danz 2021년 4월 7일
x must be an n*m matrix where n is divisible of 4.
M is the reformatted matrix with size k*j where k=n/2 and j=m*2.
x = [1 1 1 1;
2 2 2 2;
3 3 3 3;
4 4 4 4;
5 5 5 5;
6 6 6 6;
7 7 7 7;
8 8 8 8];
rowIdx = [1 3 2 4]' + [0:4:size(x,1)-1];
M = reshape(x(rowIdx(:),:)', size(x,2)*2, [])'
M = 4×8
1 1 1 1 3 3 3 3 2 2 2 2 4 4 4 4 5 5 5 5 7 7 7 7 6 6 6 6 8 8 8 8
Another example,
x = [1 1 1 1;
2 2 2 2;
3 3 3 3;
4 4 4 4;
5 5 5 5;
6 6 6 6;
7 7 7 7;
8 8 8 8
9 9 9 9
10 10 10 10
11 11 11 11
12 12 12 12];
rowIdx = [1 3 2 4]' + [0:4:size(x,1)-1];
M = reshape(x(rowIdx(:),:)', size(x,2)*2, [])'
M = 6×8
1 1 1 1 3 3 3 3 2 2 2 2 4 4 4 4 5 5 5 5 7 7 7 7 6 6 6 6 8 8 8 8 9 9 9 9 11 11 11 11 10 10 10 10 12 12 12 12
To get from M back to x,
k = reshape(M',size(M,2)/2,[]);
rowIdx = [1 3 2 4]' + (0:4:size(k,2)-1);
xOrig = k(:,rowIdx(:))';
  댓글 수: 4
Adam Danz
Adam Danz 2021년 4월 7일
See updated solution.
Pascal Weller
Pascal Weller 2021년 4월 7일
Amazing! Huge thanks! You make it look so easy...

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Taimoor Hasan Khan
Taimoor Hasan Khan 2021년 3월 23일
A = [1 1 1 1;
2 2 2 2;
3 3 3 3;
4 4 4 4;
5 5 5 5;
6 6 6 6;
7 7 7 7;
8 8 8 8];
A = cat( 2, [ A(1:2, :); A(5:6, :) ], [ A(3:4, :); A(7:8, :) ] )
% vertical concat. #1 % vertical concat #2
Not sure how you could generalize the process without loops though, sorry...
  댓글 수: 1
Pascal Weller
Pascal Weller 2021년 3월 31일
In my case the matrix is some million rows long so I would definitely need to do the indexing with the help of a for loop. However, for smaller matrices your answer is a simple solution and much appreciated. I am sure it will be of good use for someone out there. So thanks for your contribution!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by