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

15 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2018-9-24
编辑:Stephen23 2018-9-24
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 个评论
Stephen23
Stephen23 2018-9-24
编辑:Stephen23 2018-9-24
"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 2018-9-24
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!

请先登录,再进行评论。

更多回答(1 个)

jonas
jonas 2018-9-24
编辑:jonas 2018-9-24
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 个评论
jonas
jonas 2018-9-24
编辑:jonas 2018-9-24
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 2018-9-24
编辑:Kyle Gao 2018-9-24
Thank you jonas! Stephen's approach suits my problem better. But your idea definitely also helped me a lot!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by