How to reshape a matrix

2 次查看(过去 30 天)
carlas
carlas 2011-11-2
Hello,
For example I would like to reshape the following matrix
1 0 0 2 0 0
0 1 0 0 2 0
to
1 0 0
0 1 0
2 0 0
0 2 0
The number of horizontally stacked matrices is now 2,so the solution is easy b = [a(1:2,:); a(3:end,:)];
The question is now how to do this for n horizontally stacked matrices without using a loop? Does someone know?
Kind regards, Carlas

采纳的回答

Jan
Jan 2011-11-2
A = [1 0 0 2 0 0; 0 1 0 0 2 0];
B = reshape(permute(reshape(A, 2, 3, 2), [1, 3, 2]), 4, 3)

更多回答(2 个)

Amith Kamath
Amith Kamath 2011-11-2
if A is the first matrix that you defined, do:
B = [A(1:2,1:3);A(1:2,4:6)] to choose the required rows and columns from A to define B.

carlas
carlas 2011-11-2
a(:,:,1)=eye(3,2);
a(:,:,2)=2.*eye(3,2);
B = reshape(permute(a, [1, 3, 2]), size(a,3)*size(a,1),size(a,2))

类别

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