How to reshape 3D matrix ?
96 次查看(过去 30 天)
显示 更早的评论
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
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)
Otherwise, you'll have to explain how you want the data to be oriented.
采纳的回答
更多回答(1 个)
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])
% 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 个评论
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]);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!