How do I convert a 2d matrix to a 3d matrix?

81 ビュー (過去 30 日間)
Linjun He
Linjun He 2018 年 12 月 26 日
編集済み: Linjun He 2018 年 12 月 26 日
A(:,:,1) = ones(2,4);
A(:,:,2) = 2*ones(2,4);
A(:,:,3) = 3*ones(2,4);
C = [ones(2,4);2*ones(2,4);3*ones(2,4)];
In this post and this post, converting 3d matrix A to 2d matrix C is discussed.
However, how do I convert 2d matrix C back to 3d matrix A?
A(:,:,1) =
1 1 1 1
1 1 1 1
A(:,:,2) =
2 2 2 2
2 2 2 2
A(:,:,3) =
3 3 3 3
3 3 3 3
C =
1 1 1 1
1 1 1 1
2 2 2 2
2 2 2 2
3 3 3 3
3 3 3 3

採用された回答

Akira Agata
Akira Agata 2018 年 12 月 26 日
It's time to use reshape function! Please try the following:
A = reshape(C',[2,3,3]);
  1 件のコメント
Linjun He
Linjun He 2018 年 12 月 26 日
I modified the question. And now the answer from @Akira is
A = reshape(C',[2,4,3]);
It works well in this case.
Thank you!

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

その他の回答 (1 件)

Linjun He
Linjun He 2018 年 12 月 26 日
編集済み: Linjun He 2018 年 12 月 26 日
Similarly, I find this works:
permute(reshape(C, 2, 3, 4), [1 3 2])
If you find answer from @Akira does not work in your case, you can refer to this answer :p
And this answer is from @Titus

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by