How to find x value for a known y in array plot?
古いコメントを表示
I have to draw a plot based on a long matrix, which I did, after that I got the maximum of y value (max(y)) but I need the x value for the known y value y=0.8*max(y) THIS y value is not included in the matrix, so I can't use the Index of the maximum to get my x
I did found a similar question https://de.mathworks.com/matlabcentral/answers/258556-how-to-i-find-a-x-value-from-a-given-y
and tried to apply the same code x(y==0.8*max(y)) but I get a 0x1 empty column vector.
Can anyone help me out? thanks in advance
採用された回答
その他の回答 (4 件)
David Hill
2022 年 8 月 7 日
[~,idx]=min(abs(y-.8*max(y)));%.8*max(y) is not part of the y-array, find the closest y-idx to that value
x(idx)
Bruno Luong
2022 年 8 月 10 日
編集済み: Bruno Luong
2022 年 8 月 10 日
*
x = linspace(-3,3);
Assuming your data are monotonic on the neighborhood of the max
y = exp(-x.^2);
[maxy, imax] = max(y);
yt = 0.8*maxy;
% left side
i1 = find(y<yt & 1:length(y)<imax, 1, 'last');
if isempty(i1)
error('no x found on the left side')
end
xl = interp1(y([i1 i1+1]), x([i1 i1+1]), yt)
% right side
i2 = find(y<yt & 1:length(y)>=imax, 1, 'first');
if isempty(i2)
error('no x found on the right side')
end
xr = interp1(y([i2-1 i2]), x([i2-1 i2]), yt)
カテゴリ
ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

