How to reshape matrix in this way?

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.

Antworten (1)

Roger Stafford
Roger Stafford am 13 Nov. 2014
Bearbeitet: Roger Stafford am 13 Nov. 2014

0 Stimmen

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);

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 12 Nov. 2014

Bearbeitet:

am 13 Nov. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by