Vectorising a Meshgrid Coordinate Construction
Mostrar comentarios más antiguos
I have three coordinate arrays (file attached) of size [M x 2], where M is the number of elements and the 2 represents different points in a 1D system. To find the 3D meshed coordinates upon which I perform calculations I use code of the form:
for ele = 1:size(X_co,1) %Loop Over Each Element
[X_cube,Y_cube,Z_cube] = meshgrid(X_co(ele,:),Y_co(ele,:),Z_co(ele,:)); %Construct Meshgrid of size [2x2x2] for an Element
X_save(:,:,:,element) = X_cube; %Save Coordinates
Y_save(:,:,:,element) = Y_cube;
Z_save(:,:,:,element) = Z_cube;
end
The solution then is of size [2x2x2xM]. My best attempt to vectorise this was:
[X_cube,Y_cube,Z_cube] = meshgrid(X_co,Y_co,Z_co);
X_save = reshape(X_cube,2,2,2,[]); Y_save = reshape(Y_cube,2,2,2,[]); Z_save = reshape(Z_cube,2,2,2,[]);
However this solution is of size [2x2x2x(M^3)] which, when plotting both solutions has many many many repeated points so doesn't actually work for my application.
Can this problem be vectorised, or is a for loop the best way to construct this coordinate array?
3 comentarios
KSSV
el 2 de Sept. de 2020
Are you looking for ndgrid?
jessupj
el 2 de Sept. de 2020
ndgrid is the way to go here i think. i think it can be done using meshgrid along with some resize + repmat statements.
ADSW121365
el 2 de Sept. de 2020
Editada: ADSW121365
el 2 de Sept. de 2020
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Graphics Performance en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!