How to retrieve x, y values from surf given z?

17 visualizaciones (últimos 30 días)
Kyle Gao
Kyle Gao el 24 de Sept. de 2018
Editada: Stephen23 el 24 de Sept. de 2018
I got surf by using
s = surf(X, Y, Z);
I know I can use xq, yq, and inter2 to retrieve zq from the surf, like following
zq = interp2(X, Y, Z, xq, yq);
but how to get all the x,y on the surf where z is a certain value.

Respuesta aceptada

Stephen23
Stephen23 el 24 de Sept. de 2018
Editada: Stephen23 el 24 de Sept. de 2018
You could use contour or contourc or contour3 for what you want, but this is much more challenging than you think. Consider a 3D plane or 3D curve without any discontinuities: for some Z on that plane/curve, there could be infinite X and Y values that have the same Z value: these are the contours of that plane/curve. In some degenerate cases there may be only one X or Y value.
So which if those infinite/one X and Y values do you want? Think about contours, and then think about how such results could possibly be encoded or returned. Read the contour and contourc help. Read about the contour matrix:
Here is a simple example of how it works:
[X,Y,Z] = peaks(25);
surf(X,Y,Z)
Xg = X(1,:);
Yg = Y(:,1);
hold on
M = contour3(Xg,Yg,Z,[5,5],'*-r');
%
fun = @(s,v)fprintf('%s %s\n',s,sprintf(' %5.2f',v));
% display grid:
fun('Xg',Xg)
fun('Yg',Yg)
% display contour:
fun('Xc',M(1,2:end))
fun('Yc',M(2,2:end))
Original grid:
Xv -3.00 -2.75 -2.50 -2.25 -2.00 -1.75 -1.50 -1.25 -1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00
Yv -3.00 -2.75 -2.50 -2.25 -2.00 -1.75 -1.50 -1.25 -1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00
Contour matrix for Z==5:
Xc -0.48 -0.25 0.00 0.25 0.50 0.50 0.70 0.66 0.50 0.38 0.25 0.00 -0.25 -0.38 -0.50 -0.64 -0.66 -0.50 -0.48
Yc 1.25 1.14 1.12 1.16 1.25 1.25 1.50 1.75 1.92 2.00 2.06 2.10 2.06 2.00 1.92 1.75 1.50 1.27 1.25
The contour for Z==5 is plotted in red:
  4 comentarios
Stephen23
Stephen23 el 24 de Sept. de 2018
Editada: Stephen23 el 24 de Sept. de 2018
"I have thought about this, I think like interp2, the output is given based on the grid"
That is basically what the contour family does. But you need to consider that where any specific Z value is probably won't correspond to both gridded X and Y values at the same time. Generally speaking, it is very unlikely that both the X and Y values that correspond to your specific Z value will both be on their grid. So what the contour family does is find the gridded X values for that Z values, and the gridded Y values for that Z value, and gives you that combined set.
See the example in my answer: I tried to make it clear that the contour matrix includes either X or Y gridded values, by printing the values aligned in columns. Not that if you have multiple distinct contours then the contour matrix is more complex: read the link I gave you very carefully!
Kyle Gao
Kyle Gao el 24 de Sept. de 2018
Thank you so much! I think this is what I want, I still need to try this on my dataset. Hope it works! Thanks a lot!

Iniciar sesión para comentar.

Más respuestas (1)

jonas
jonas el 24 de Sept. de 2018
Editada: jonas el 24 de Sept. de 2018
Well, you can use
xq(zq==v)
xq(zq==v)
to find the points ( xq & yq ) where zq is equal to some specific value, v, but you're going to run into problems with comparing floats. A better approach would be to specify some tolerance, tol
xq(abs(zq-v)<tol)
yq(abs(zq-v)<tol)
  4 comentarios
jonas
jonas el 24 de Sept. de 2018
Editada: jonas el 24 de Sept. de 2018
I don't want to hijack Stephens answer, so I'll just leave another reply here.
" Actually, I want to get the [xy] within the area where zq-tol < zq < zq+tol "
This is exactly what
abs(zq-v)<tol
does... you obtain all values for zq +- tol. But honestly, the other answer is better, as it does not require any tolerance. The interpolation is kinda of integrated with the contour plot.
Kyle Gao
Kyle Gao el 24 de Sept. de 2018
Editada: Kyle Gao el 24 de Sept. de 2018
Thank you jonas! Stephen's approach suits my problem better. But your idea definitely also helped me a lot!

Iniciar sesión para comentar.

Categorías

Más información sobre Contour Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by