How to find x value for a known y in array plot?
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (4)
David Hill
le 7 Août 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)
1 commentaire
amoda
le 10 Août 2022
amoda
le 10 Août 2022
0 votes
Bruno Luong
le 10 Août 2022
Modifié(e) : Bruno Luong
le 10 Août 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
le 14 Août 2022
0 votes
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

