Suppose I have a 2D matrix (6,3) as follows
A
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
Now I want to reshape into one 3D matrix (3,3,2) like as follows:
A_3D(:, :, 1)
1 2 3
4 5 6
7 8 9
A_3D(:, :, 2)
10 11 12
13 14 15
16 17 18
How I can achieve that with an efficient way?
Thank you

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 10월 2일
편집: Ameer Hamza 2020년 10월 2일

0 개 추천

Combination of reshape and permute
permute(reshape(A, 3, size(A,1)/3, []), [1 3 2])

댓글 수: 4

Priyabrata Das
Priyabrata Das 2020년 10월 2일
편집: Priyabrata Das 2020년 10월 2일
Thank you Mr. Ameer. This code is working fine for the given example. Suposse I have a 2D matrix of (3N,3) dimension and I want to covert into a 3D matrix of (3,3,N) dimension as per the above mentioned fashion. Will this code work too? If not, then what should be the proper way?
Thank you!
Ameer Hamza
Ameer Hamza 2020년 10월 2일
See the updated answer.
Priyabrata Das
Priyabrata Das 2020년 10월 3일
It is working fine. Now I have to understand the logic behind this line of code. Thank you very much.
Ameer Hamza
Ameer Hamza 2020년 10월 3일
I am glad to be of help. You can run each part one by one and see the output. That will help in understanding.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2020년 10월 2일

댓글:

2020년 10월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by