How to convert a point in meshgrid to vector coordinates?
32 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aryana
el 27 de En. de 2023
Comentada: Aryana
el 29 de En. de 2023
I have a meshgrid [X,Y,Z] = meshgrid(x,y,z). Let's say the meshgrid is size K*K*K and each of X,Y,Z are individually also of this size. How do I go from a point in the meshgrid to coordinates of X/Y/Z? Sorry if that doesn't make any sense but maybe that is why I am confused!
2 comentarios
KSSV
el 27 de En. de 2023
What do you mean by that? Can you show us any example or pictorial example?
Respuesta aceptada
John D'Errico
el 27 de En. de 2023
Editada: John D'Errico
el 27 de En. de 2023
You already have the coordinates, so I'm not at all sure what your question asks. Perhaps an example would help.
x = -10:5:10
y = 0:5:25
z = -20:10:20
Now use meshgrid.
[X,Y,Z] = meshgrid(x,y,z);
This creates 3 arrays, each of size
size(X)
So y varies the fastest. (Personally, this has always bugged me, so I have always preferred ndgrid. The difference is not really that important though, and there was a valid reason for that choice.) X looks like this:
X
So each array has 3 dimensions. But we can also convert them to a list of (x,y,z) triads, as:
XYZ = [X(:),Y(:),Z(:)]
Each row of the resulting array corresponds to one (x,y,z) triple. There will be 5x6x5=150 of those triples.
size(XYZ)
Anyway, it will help once you start learning to use and write vectorized codes, where X, Y, and Z are all arrays. In that sense (X,Y,Z) really are the vector coordinates you are looking for.
Más respuestas (1)
Benjamin Campbell
el 27 de En. de 2023
Presuming you start with vectors x, y and z. When you do [X Y Z] = meshgrid(x, y, z) you get three 3D arrays with every combination. To answer the question it depends on how you get the indices (x1 y1 z1). If you get them from the original vectors, you can just do x(x1) y(y1) z(z1). Or, if you get them from the meshgrid values it will be X(x1, y1, z1) Y(x1, y1, z1) Z(x1, y1, z1)
If you share code it will be easier to answer, but hope that helps.
Ver también
Categorías
Más información sobre Surface and Mesh Plots en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!