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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by