How to find x values of specified y point on the graph ?
109 ビュー (過去 30 日間)
古いコメントを表示
Let say i have two array x[1,2,3,4,5] and y[6, 7,8,9,10 ] i want to find the x values of the 6.34 on the graph. I want to put marker for that point is there any way to do it?
0 件のコメント
回答 (3 件)
Voss
2022 年 7 月 19 日
x = [1,2,3,4,5];
y = [6,7,8,9,10];
y_point = 6.34;
x_point = interp1(y,x,y_point);
plot(x,y);
hold on
plot(x_point,y_point,'r.')
4 件のコメント
erin
2025 年 2 月 5 日
I seem to be having a problem where I input the code but the point isn't actually on the line of the graph
Voss
2025 年 2 月 5 日
@erin: You may need to interpolate over multiple segments separately:
x = [1,2,3,4,5,6,7,8,9,10];
y = [6,7,8,9,10,9.5,8.5,7.5,6.5,5.5];
y_point = 6.34;
idx = find(diff(sign(y-y_point)));
n = numel(idx);
xi = zeros(1,n);
yi = zeros(1,n);
nx = numel(x);
for ii = 1:n
tmp = max(1,idx(ii)-1):min(nx,idx(ii)+1);
xi(ii) = interp1(y(tmp),x(tmp),y_point);
yi(ii) = y_point;
end
plot(x,y,'o-');
hold on
plot(xi,yi,'r.')
Star Strider
2022 年 7 月 19 日
編集済み: Star Strider
2024 年 11 月 26 日
Using the supplied .fig file —
F = openfig('graph1.fig');
Lines = findobj(gca, 'Type','line');
x = Lines.XData;
y = Lines.YData;
yval = 0.0100277; % Choose A Value Within tThe Range Of 'y'
yxi = find(diff(sign(y-yval)));
for k = 1:numel(yxi)
idxrng = max(1,yxi(k)-1) : min(numel(y),yxi(k)+1);
xv(k) = interp1(y(idxrng), x(idxrng),yval);
yv(k) = yval;
end
% figure
% plot(x, y)
hold on
plot(xv, yv, 'rs', 'MarkerSize',10)
hold off
EDIT — (26 Nov 2024 at 12:05)
Ran code here. This was not possible when this post first appeared.
.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


