How to find x value for a known y in array plot?
Show older comments
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
Accepted Answer
More Answers (4)
David Hill
on 7 Aug 2022
[~,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)
amoda
on 10 Aug 2022
0 votes
Bruno Luong
on 10 Aug 2022
Edited: Bruno Luong
on 10 Aug 2022
*
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)
amoda
on 14 Aug 2022
0 votes
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
