Reshaping array with specific order

3 次查看(过去 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