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 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Azzi Abdelmalek
2014-3-30
编辑:Azzi Abdelmalek
2014-3-30
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])
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!