Reshape a 3D matrix

3 views (last 30 days)
gianluca
gianluca on 4 Oct 2019
Commented: Stephen23 on 5 Oct 2019
I have 3 vectors
x = [1 2 3];
y = [4 5 6];
z = [7 8 9];
from which I create a meshgrid (I use this 3D mesh to compute some values so that I have also a 3D matrix A of size 3-by-3-by-3
[X,Y,Z] = meshgrid(x,y,z);
I want reshape the meshgrid and A to obtain a matrix (x y z value of A) of dimension size(x).*size(y).*size(z)-by-4, like that:
A = [1 4 7 A(1,1,1)
2 4 7 A(2,1,1)
3 4 7 A(3,1,1)
1 5 7 A(1,2,1)
2 5 7 A(2,2,1)
3 5 7 A(3,2,1)
1 6 7 A(1,3,1)
2 6 7 A(2,3,1)
3 6 7 A(3,3,1)
1 4 8 A(1,1,2)
2 4 8 A(2,1,2)
3 4 8 A(3,1,2)
1 5 8 A(1,2,2)
2 5 8 A(2,2,2)
3 5 8 A(3,2,2)
1 6 8 A(1,3,2)
2 6 8 A(2,3,2)
3 6 8 A(3,3,2)
1 4 9 A(1,1,3)
2 4 9 A(2,1,3)
3 4 9 A(3,1,3)
1 5 9 A(1,2,3)
2 5 9 A(2,2,3)
3 5 9 A(3,2,3)
1 6 9 A(1,3,3)
2 6 9 A(2,3,3)
3 6 9 A(3,3,3)]

Answers (2)

Rik
Rik on 4 Oct 2019
The code below should do what you want:
x = [1 2 3];
y = [4 5 6];
z = [7 8 9];
[X,Y,Z] = meshgrid(x,y,z);
A=round(rand*X+rand*Y.^2+sqrt(rand*Z));
X=permute(X,[2 1 3]);
Y=permute(Y,[2 1 3]);
Z=permute(Z,[2 1 3]);
A=permute(A,[2 1 3]);
A_out=[X(:) Y(:) Z(:) A(:)];
  1 Comment
Stephen23
Stephen23 on 5 Oct 2019
Edited: Stephen23 on 5 Oct 2019
"The code below should do what you want:"
Actually it doesn't do what was asked**: it returns the values of A in a different order to that requested by the question, giving:
1 4 7 A(1,1,1)
2 4 7 A(1,2,1)
3 4 7 A(1,3,1)
1 5 7 A(2,1,1)
2 5 7 A(2,2,1)
...
Removing the A permute resolves this bug.
** of course what the OP asked for and what they want could easily be different...

Sign in to comment.


Stephen23
Stephen23 on 5 Oct 2019
Rather than using meshgrid it would be simpler to use ndgrid:
>> x = [1 2 3];
>> y = [4 5 6];
>> z = [7 8 9];
>> A = randi(9,3,3,3)
A(:,:,1) =
1 9 5
2 9 8
9 2 2
A(:,:,2) =
1 1 9
4 8 8
2 3 1
A(:,:,3) =
2 4 7
4 9 7
1 5 8
>> [X,Y,Z] = ndgrid(x,y,z);
>> B = [X(:),Y(:),Z(:),A(:)]
B =
1 4 7 1
2 4 7 2
3 4 7 9
1 5 7 9
2 5 7 9
3 5 7 2
1 6 7 5
2 6 7 8
3 6 7 2
1 4 8 1
2 4 8 4
3 4 8 2
1 5 8 1
2 5 8 8
3 5 8 3
1 6 8 9
2 6 8 8
3 6 8 1
1 4 9 2
2 4 9 4
3 4 9 1
1 5 9 4
2 5 9 9
3 5 9 5
1 6 9 7
2 6 9 7
3 6 9 8
  4 Comments
Rik
Rik on 5 Oct 2019
Simpler code is less buggy (as the answers on this thread demonstrate).
I agree with the first part of that comment, but not the second part. By the description ("I use this 3D mesh to compute some values so that I have also a 3D matrix A of size 3-by-3-by-3") I still stand by my suggested code. If you calculate something based on all combinations of inputs, then it makes sense to want all inputs and output on the same line, and not de-couple them.
My answer may not be as described, but it is not a bug. If you disagree, please explain your reasons to call it buggy.
And for the transpose: of course I'm aware permute(matrix,[2 1 3]) is not officially a transpose, but since both will flip the first two dimensions I think it is fine to call it that informally.
Stephen23
Stephen23 on 5 Oct 2019
"If you disagree, please explain your reasons to call it buggy."
Because it does not give the data in the requested order.

Sign in to comment.

Categories

Find more on Matrices and Arrays 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!