How to reshape 3D matrix ?
96 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
DGM
le 22 Juil 2021
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)
Otherwise, you'll have to explain how you want the data to be oriented.
Réponse acceptée
Stephen23
le 22 Juil 2021
A = randi(9,4,4,3)
B = permute(reshape(permute(A,[2,1,3]),[2,8,3]),[3,1,2])
3 commentaires
Stephen23
le 22 Juil 2021
"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])
% ^ ^^^ ^
Plus de réponses (1)
Chunru
le 22 Juil 2021
% Generate data (using number 1,...48 to represent a,...V)
Z = permute(reshape(1:48, [4 4 3]), [2 1 3])
% First permute the dimension so that a,b,c,.. are along the 1st dim
Z1 = permute(Z, [2 1 3])
% No reshape
Z2 = reshape(Z1, [2 8 3])
% permute
Z3 = permute(Z2, [3 1 2])
3 commentaires
Chunru
le 22 Juil 2021
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]);
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!