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

15 Ansichten (letzte 30 Tage)
Kyle Gao
Kyle Gao am 24 Sep. 2018
Bearbeitet: Stephen23 am 24 Sep. 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.

Akzeptierte Antwort

Stephen23
Stephen23 am 24 Sep. 2018
Bearbeitet: Stephen23 am 24 Sep. 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 Kommentare
Stephen23
Stephen23 am 24 Sep. 2018
Bearbeitet: Stephen23 am 24 Sep. 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 am 24 Sep. 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

jonas
jonas am 24 Sep. 2018
Bearbeitet: jonas am 24 Sep. 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 Kommentare
jonas
jonas am 24 Sep. 2018
Bearbeitet: jonas am 24 Sep. 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 am 24 Sep. 2018
Bearbeitet: Kyle Gao am 24 Sep. 2018
Thank you jonas! Stephen's approach suits my problem better. But your idea definitely also helped me a lot!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Contour Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by