Form a 3D matrix for a 2d matrix

1 view (last 30 days)
Luis Isaac
Luis Isaac on 13 Nov 2019
Commented: Matt J on 13 Nov 2019
Dear
I have a 3d matrix M3D in which each dimension represent one of the spatial coordinates x, y, z. This spatial coordinates are in three vectors xVector, yVector and zVector.
I need to perform a calculation using a function which need these coordinates SomeFun(x,y,z). The function is vectored so for a matrix with n points and 3 columns, each row represent three coordinates, gives a vector of results.
To form this such of matrix from spatial vectors I use the matlab function “combvec”:
EntranceMatrix=combvec(xVector, yVector, zVector)';
ExitVector=SomeFun(EntranceMatrix);
The result is an ExitVector so the ExitVector(i) is the result of EntranceMatrix(i,:)
I would like to put ExitVector in the same form of the original 3d Matrix, i.e. form EntranceVector and ExitVector form a NewM3D, so every element NewM3D(i,j,k) has the same spacial correspondence to M3D(i,j,k).
How can I do it?
Thanks in advance

Answers (1)

Matt J
Matt J on 13 Nov 2019
Edited: Matt J on 13 Nov 2019
NewM3D = reshape(ExitVector,size(M3D));
  2 Comments
Matt J
Matt J on 13 Nov 2019
Luis' comment moved here:
Thanks very much
But there is a problem in my quetion, I forget to say that xVector represent the first dimension in M3D, yVector is the third dimension (not the second) and zVector is the second dimension.
So If M3D is a matrix 40 x 57 x 60 then xVetor is a vectror of 40 elements, yVector has 60 and zVector has 57.
I think that reshape gives the correct answared if yVector was related to the second dimension of M3D
Matt J
Matt J on 13 Nov 2019
[m,n,p]=size(M3D);
NewM3D = permute( reshape(ExitVector,[m,p,n]) ,[1,3,2]);

Sign in to comment.

Categories

Find more on Computational Geometry in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!