Need to find a specific point on a line using data from a 2 column matrix

34 ビュー (過去 30 日間)
erin
erin 2025 年 2 月 5 日 18:14
コメント済み: erin 2025 年 2 月 5 日 23:18
The variable WL is a 2686x3 matrix
the first plot is what I'm working with which plots wl(:,1) against wl(:,2) (the blue line)
my problem is that I have a value in wl(:,2) equal to 4.12332783. I need to find the corresponding value in wl(:,1).
The issue is that although I'm using the interp1 commmand correctly, the point it gives me is not on the line where it needs to be for some reason. I'll attach a picture of the graph for reference.
clc
clear
load 'bumpt test.mat'
x=wl(:,1);
y=wl(:,2);
figure (1)
plot (x,y)
hold on
plot (wl(:,1),wl(:,3))
[ymin imin]=min(wl(:,2));
xmin=x(imin);
style='ro';
markersize=10;
plot(x(imin),ymin,style,'MarkerSize',markersize)
[ymax imax]=max(wl(:,2));
xmax=x(imax);
plot(x(imax),ymax,style,'MarkerSize',markersize)
y0=4.12332783;
x_point=interp1(wl(:,2),wl(:,1),4.12332783);
plot(x_point,y0,style,'MarkerSize',markersize)
  2 件のコメント
dpb
dpb 2025 年 2 月 5 日 21:26
Attach the .mat file; can't really diagnose just looking at the picture; particularly since you don't distinguish which red circle is which nor explain exactly what you were expecting.
erin
erin 2025 年 2 月 5 日 23:18
My apologies. The red circles attached to the blue lines are the max and min values and the red circle in the middle is the one being a problem. The y coordinate for the data in w(:,2) is supposed to be 4.12332783 and I need to find the cooresponding value in w(:,1). The issue is that i need the x-coordinate point on the blue line on the graph where y=4.12332783.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2025 年 2 月 5 日 23:10
Your y data has multiple different x values for most y values. Interpolating using y as the independent variable and x as the dependant variable is almost certain to give unexpected results. You have at least three different places where y crosses 4.12332783 and the way you are doing the interpolation you could get any of those locations (and possibly a strange location if interp1() effectively sorts the independant coordinate first.)
You need to isolate ranges. For example,
target = 4.12332783;
mask = sign(y - target).';
mask = (y(1:end-1) <= target & y(2:end) >= target) | ...
(y(1:end-1) >= target & y(2:end) <= target));
search_positions = find(mask);
Now the target was crossed between y(search_positions) and y(search_positions + 1).
Perhaps knowing that is close enough, without bothering to interp1 to get a more exact position.

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by