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"

2 views (last 30 days)
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!

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 30 Mar 2014
Edited: Azzi Abdelmalek on 30 Mar 2014
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])

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!