Reshape matrix with multiple columns into 2 columns
2 次查看(过去 30 天)
显示 更早的评论
Noticed that matlab has function of reshaping matrix into specific size, but is it possible to move 2 by 2 columns, for exapmle, 3 & 4th columns below 1 & 2nd columns, so far so on?
From A to B:
A = [1 2 3 4;5 6 7 8;9 10 11 12]
B = [1 2;5 6;9 10;3 4;7 8;11 12]
Thank you =]
0 个评论
采纳的回答
KSSV
2022-3-10
A = [1 2 3 4;5 6 7 8;9 10 11 12]
B = [1 2;5 6;9 10;3 4;7 8;11 12]
iwant = [A(:,1:2) ; A(:,3:4)]
更多回答(1 个)
Stephen23
2022-3-10
编辑:Stephen23
2022-3-10
Of course, here are two general solutions.
A = [1,2,3,4;5,6,7,8;9,10,11,12]
B = [1,2;5,6;9,10;3,4;7,8;11,12] % desired output
Method one: MAT2CELL and concatentation:
C = mat2cell(A,3,[2,2]);
B = vertcat(C{:})
Method two: use RESHAPE and PERMUTE.
B = reshape(permute(reshape(A.',2,2,3),[1,3,2]),2,[]).'
You do not need to use a loop!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!