How to reshape 3D matrix ?

124 ビュー (過去 30 日間)
SojM
SojM 2021 年 7 月 22 日
コメント済み: SojM 2021 年 7 月 22 日
I have a 3D matrix of 36*42*7 dimension. I want to reshape it in such a way that I extract two colums from each third dimension. That means my final matrix dimension would be 7*2*756. How can I do this?
For example, Assume I have 4*4*3 matrix
A1= [a b c d; e f g h; i j k l; m n o p]
A2= [q r s t; u v w x; y z A B; C D E F]
A3= [G H I J; K L M N; O P Q R; S T U V ]
Now I want to reshape it to 3*2*8 such that
B1= [a b; q r; G H]
B2=[c d; s t; I J]
B3=[e f; u v; K L]
B4..... B8
  2 件のコメント
DGM
DGM 2021 年 7 月 22 日
Your question is ambiguous. There are a number of ways 10584 elements can be reshaped from one array to another with the same number of elements. If one assumes that everything should be columnwise, then you can just use reshape()
A = rand(36,42,7);
B = reshape(A,7,2,756);
size(B)
ans = 1×3
7 2 756
Otherwise, you'll have to explain how you want the data to be oriented.
SojM
SojM 2021 年 7 月 22 日
I have edited the question with example.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2021 年 7 月 22 日
A = randi(9,4,4,3)
A =
A(:,:,1) = 5 7 6 6 7 8 8 1 7 3 8 7 5 1 5 4 A(:,:,2) = 5 6 2 4 2 9 6 2 3 3 2 2 5 1 5 6 A(:,:,3) = 6 9 8 5 1 7 7 9 5 8 5 3 6 5 5 9
B = permute(reshape(permute(A,[2,1,3]),[2,8,3]),[3,1,2])
B =
B(:,:,1) = 5 7 5 6 6 9 B(:,:,2) = 6 6 2 4 8 5 B(:,:,3) = 7 8 2 9 1 7 B(:,:,4) = 8 1 6 2 7 9 B(:,:,5) = 7 3 3 3 5 8 B(:,:,6) = 8 7 2 2 5 3 B(:,:,7) = 5 1 5 1 6 5 B(:,:,8) = 5 4 5 6 5 9
  3 件のコメント
Stephen23
Stephen23 2021 年 7 月 22 日
"How can it be done for large matrix of 36*42*7 to reshpae it into 7*2*756 in a simlar way?"
B = permute(reshape(permute(A,[2,1,3]),[2,756,7]),[3,1,2])
% ^ ^^^ ^
SojM
SojM 2021 年 7 月 22 日
Thanks! It works

サインインしてコメントする。

その他の回答 (1 件)

Chunru
Chunru 2021 年 7 月 22 日
% Generate data (using number 1,...48 to represent a,...V)
Z = permute(reshape(1:48, [4 4 3]), [2 1 3])
Z =
Z(:,:,1) = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Z(:,:,2) = 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Z(:,:,3) = 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
% First permute the dimension so that a,b,c,.. are along the 1st dim
Z1 = permute(Z, [2 1 3])
Z1 =
Z1(:,:,1) = 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 Z1(:,:,2) = 17 21 25 29 18 22 26 30 19 23 27 31 20 24 28 32 Z1(:,:,3) = 33 37 41 45 34 38 42 46 35 39 43 47 36 40 44 48
% No reshape
Z2 = reshape(Z1, [2 8 3])
Z2 =
Z2(:,:,1) = 1 3 5 7 9 11 13 15 2 4 6 8 10 12 14 16 Z2(:,:,2) = 17 19 21 23 25 27 29 31 18 20 22 24 26 28 30 32 Z2(:,:,3) = 33 35 37 39 41 43 45 47 34 36 38 40 42 44 46 48
% permute
Z3 = permute(Z2, [3 1 2])
Z3 =
Z3(:,:,1) = 1 2 17 18 33 34 Z3(:,:,2) = 3 4 19 20 35 36 Z3(:,:,3) = 5 6 21 22 37 38 Z3(:,:,4) = 7 8 23 24 39 40 Z3(:,:,5) = 9 10 25 26 41 42 Z3(:,:,6) = 11 12 27 28 43 44 Z3(:,:,7) = 13 14 29 30 45 46 Z3(:,:,8) = 15 16 31 32 47 48
  3 件のコメント
Chunru
Chunru 2021 年 7 月 22 日
Exactly same:
Z = rand(36, 42, 7);
Z1 = permute(Z, [2 1 3]);
Z2 = reshape(Z1, [2 756 7]); %2x756 = 36*42
Z3 = permute(Z2, [3 1 2]);
Or you can combine the three statements in on as in @Stephen Cobeldick's answer.
SojM
SojM 2021 年 7 月 22 日
Thanks. It worked.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by