Reshaping array with specific order

조회 수: 4 (최근 30일)
Prabhjot Dhami
Prabhjot Dhami 2021년 10월 18일
편집: dpb 2021년 10월 18일
Greetings,
I have a 2-D matrix that I would like to reshape into a 3-D array with specific ordering of the elements.
For example, if my 2-D matrix is
A = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
B = reshape(A, 2, 5, [])
ans(:,:,1) =
1 2 3 4 5
11 12 13 14 15
ans(:,:,2) =
6 7 8 9 10
16 17 18 19 20
However, the order I am looking for through reshape is:
ans(:,:,1) =
1 2 3 4 5
6 7 8 9 10
ans(:,:,2) =
11 12 13 14 15
16 17 18 19 20
Any help with how I can do this?
Thank you.
  댓글 수: 1
Chunru
Chunru 2021년 10월 18일
Are you sure you have correct description of your problem?
A = [1 2 3 4 5; 6 7 8 9 10];
B = reshape(A, 2, 5, [])
B = 2×5
1 2 3 4 5 6 7 8 9 10

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

답변 (1개)

dpb
dpb 2021년 10월 18일
편집: dpb 2021년 10월 18일
Can't be done with only reshape. You have only 10 elements in A and the requested output array requires 20.
OTOH, it's easy enough to produce the desired result by
>> A = [1 2 3 4 5; 6 7 8 9 10];
>> C=cat(3,A,A+10)
C(:,:,1) =
1 2 3 4 5
6 7 8 9 10
C(:,:,2) =
11 12 13 14 15
16 17 18 19 20
>>

카테고리

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