MATLAB: Convert 3D matrix (X by Y by Z) into 2D (3 by (X+Y+Z))

MATLABmatrix manipulation

Hi,
I would be super grateful for some help in converting a 3D matrix (X by Y by Z) into 2D (3 by (X+Y+Z)). Basically I would like to get back the 3D positions of each grid points, where the first line (in the 2D matrix) is X, the second is Y and the third is Z.
For example: xgrid = 0:100:900; ygrid = -100:50:0; zgrid = -100:50:0; [Xgrid,Ygrid,Zgrid]=meshgrid(xgrid,ygrid,zgrid);
A(1,:) % X coordinates of points in the meshgrid A(2,:) % Y coordinates of points in the meshgrid A(3,:) % Z coordinates of points in the meshgrid
The order of points does not really metter. I tried to solve it with for loops but it is just time consuming.
Thank you for any help!

Best Answer

A = [Xgrid(:),Ygrid(:),Zgrid(:)].'