Reshaping array with specific order

3 visualizzazioni (ultimi 30 giorni)
Prabhjot Dhami
Prabhjot Dhami il 18 Ott 2021
Modificato: dpb il 18 Ott 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 Commento
Chunru
Chunru il 18 Ott 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

Accedi per commentare.

Risposte (1)

dpb
dpb il 18 Ott 2021
Modificato: dpb il 18 Ott 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
>>

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by