How to reshape a 2D to 3D matrix without using loops (reshape) and keeping a particular order of the elements// Wrong arragement of elemts after using "reshape"

I've a problem using the reshape function. I've a Matrix A: A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
I want to change it to a 3D Matrix with the size 4x4x3. When I use reshape like this:
A2=repmat(A,4,4,3); the result is:
A2(:,:,1) =
1 1 1 2
5 5 5 6
9 9 9 10
13 13 13 14
A2(:,:,2) =
2 2 3 3
6 6 7 7
10 10 11 11
14 14 15 15
A2(:,:,3) =
3 4 4 4
7 8 8 8
11 12 12 12
15 16 16 16
The result I want though is:
A2(:,:,1) =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
A2(:,:,2) =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
A2(:,:,3) =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
In the real data set I've different values, I just chose (1:16) 3 Times to show in what order I want to rearrange the data/Matrix.
Thanks a lot!

 Réponse acceptée

A=[1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16]
A2=permute(reshape(A',4,4,[]),[2 1 3])

Plus de réponses (0)

Catégories

En savoir plus sur Operators and Elementary Operations dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by