How to reshape matrix in this way?

2 次查看(过去 30 天)
John
John 2014-11-12
When reshape a 3D matrix into 2D matrix, it fills columns first; for example:
a(3,3,4) =
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
3 3 3
3 3 3
3 3 3
4 4 4
4 4 4
4 4 4
reshape(a,6,6) gives:
d =
1 1 2 3 3 4
1 1 2 3 3 4
1 1 2 3 3 4
1 2 2 3 4 4
1 2 2 3 4 4
1 2 2 3 4 4
How can it be reshaped to:
d =
1 1 1 3 3 3
1 1 1 3 3 3
1 1 1 3 3 3
2 2 2 4 4 4
2 2 2 4 4 4
2 2 2 4 4 4
or
d =
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
Thanks.

回答(1 个)

Roger Stafford
Roger Stafford 2014-11-13
编辑:Roger Stafford 2014-11-13
I think this does it for the first of your desired d's:
d = reshape(permute(reshape(a,3,3,2,2),[1,3,2,4]),6,6);
Added:
The second desired d should be obtained with:
d = reshape(permute(reshape(a,3,3,2,2),[1,4,2,3]),6,6);

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by