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

조회 수: 68 (최근 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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by