How to reshape matrix in this way?
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Roger Stafford
on 13 Nov 2014
Edited: Roger Stafford
on 13 Nov 2014
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);
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!