How to convert a point in meshgrid to vector coordinates?

조회 수: 119 (최근 30일)
Aryana
Aryana 2023년 1월 27일
댓글: Aryana 2023년 1월 29일
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
KSSV
KSSV 2023년 1월 27일
What do you mean by that? Can you show us any example or pictorial example?
Aryana
Aryana 2023년 1월 27일
Well I have an element represented by indices α, β, γ in a K*K*K array and I need to find out corresponding points in my 3D meshgrid. But I may be thinking about it incorrectly and it might rely on some Fourier Transform intuition that I don't have right now.

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2023년 1월 27일
편집: John D'Errico 2023년 1월 27일
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
x = 1×5
-10 -5 0 5 10
y = 0:5:25
y = 1×6
0 5 10 15 20 25
z = -20:10:20
z = 1×5
-20 -10 0 10 20
Now use meshgrid.
[X,Y,Z] = meshgrid(x,y,z);
This creates 3 arrays, each of size
size(X)
ans = 1×3
6 5 5
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
X =
X(:,:,1) = -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 X(:,:,2) = -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 X(:,:,3) = -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 X(:,:,4) = -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 X(:,:,5) = -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10 -10 -5 0 5 10
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(:)]
XYZ = 150×3
-10 0 -20 -10 5 -20 -10 10 -20 -10 15 -20 -10 20 -20 -10 25 -20 -5 0 -20 -5 5 -20 -5 10 -20 -5 15 -20
Each row of the resulting array corresponds to one (x,y,z) triple. There will be 5x6x5=150 of those triples.
size(XYZ)
ans = 1×2
150 3
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.
  댓글 수: 1
Aryana
Aryana 2023년 1월 29일
This was very helpful thank you. I realized that what I needed to do was already accomplished when I used ind2sub to get the indices, so you are right that I already had what I needed.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Benjamin Campbell
Benjamin Campbell 2023년 1월 27일
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.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by