How to convert 1D matrix to 3D matrix with specific order.
12 views (last 30 days)
Show older comments
Eric Jiang
on 21 Jul 2016
Commented: Eric Jiang
on 22 Jul 2016
I have a 1D matrix (1 by 6,000,000) of data. I want to reshape this to a three dimensional matrix that is 100 by 100 by 600. However, when I use the reshape function, the order of the numbers comes out incorrect.
For example: (12 numbers)
a=
0.01
0.02
0.03
0.04
0.05
0.06
0.07
0.08
0.09
0.10
0.11
0.12
I want b to be: (assume 2 by 2 by 3)
b(:,:,1)=
0.01 0.07
0.04 0.10
b(:,:,2)=
0.02 0.08
0.05 0.11
b(:,:,3)=
0.03 0.09
0.06 0.12
Basically, the order that I want is if the x axis had 2 points, y axis had 3 points, and z axis had 2 points, Point 1 would be (x1, y1, z1), Point 2 would be (x1, y1, z2), Point 3 would be (x2, y1, z1), Point 4 would be (x2,y1,z2), Point 5 would be (x1, y2, z1), Point 6 would be (x1, y2,z2), Point 7 would be (x2,y2,z1), Point 8 would be (x2,y2,z2) and on to Point 12.
What is the most efficient way to do this?
Thank you.
0 Comments
Accepted Answer
Azzi Abdelmalek
on 22 Jul 2016
Edited: Azzi Abdelmalek
on 22 Jul 2016
a=0.01:0.01:0.60
%you want a nxmxp matrix with:
n=3
m=4
p=5
out=permute(reshape(a,p,n,m),[2,3,1])
You can apply this to your case 2x2x3 matrice
a=0.01:0.01:0.12
n=2
m=2
p=3
out=permute(reshape(a,p,n,m),[2,3,1])
More Answers (0)
See Also
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!