Reshaping array with specific order

3 Ansichten (letzte 30 Tage)
Prabhjot Dhami
Prabhjot Dhami am 18 Okt. 2021
Bearbeitet: dpb am 18 Okt. 2021
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 Kommentar
Chunru
Chunru am 18 Okt. 2021
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

dpb
dpb am 18 Okt. 2021
Bearbeitet: dpb am 18 Okt. 2021
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
>>

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by