Reshape matrix with multiple columns into 2 columns

2 ビュー (過去 30 日間)
lim xiang
lim xiang 2022 年 3 月 10 日
編集済み: Stephen23 2022 年 3 月 10 日
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 =]

採用された回答

KSSV
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 件のコメント
lim xiang
lim xiang 2022 年 3 月 10 日
Hi, thank you very much for your reply, realized that I can add a loop to group multiple columns of matrix

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
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]
A = 3×4
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
B = 6×2
1 2 5 6 9 10 3 4 7 8 11 12
Method one: MAT2CELL and concatentation:
C = mat2cell(A,3,[2,2]);
B = vertcat(C{:})
B = 6×2
1 2 5 6 9 10 3 4 7 8 11 12
Method two: use RESHAPE and PERMUTE.
B = reshape(permute(reshape(A.',2,2,3),[1,3,2]),2,[]).'
B = 6×2
1 2 5 6 9 10 3 4 7 8 11 12
You do not need to use a loop!

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by