Plot marker on points not specified within the vector

Hello, I have a plot made up of two vectors as such:
x = 0:1:11;
y = [0 1 2 3 4 5 4 3 2 1.5 1 0];
My goal is to create a marker on the plot where the y value reaches 75% of the max y value. I know I can find that value as such:
ymax = max(y)
y75 = 0.75*ymax
For my data, this yields a value of 3.75. I would then like to find the correlating x value for 3.75 and then create a marker at that coordinate as stated before.
I was trying to do this using: x75 = find(y75 == y)
However, the problem I have been running into is that matlab can not find the corresponding x value because that value is not specified within my x vector.
Is there a way to do this?
Thank you ahead of time!

1 comentario

Sounds like you need to fit the data, look at fit() funciton.

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 28 de Ag. de 2020
Editada: Stephen23 el 28 de Ag. de 2020
You might think this is easy, but there is no simple, general solution for an arbitrary (i.e. non-monotonic) function and an arbitrary number of intersects of that function with a line (which is what you are looking for). You can find various implementations of algorithms for finding intersects on FEX:
But first we might as well see what we can do with basic MATLAB to find the exact intersects (within numeric precision).
x = 0:1:11;
y = [0,1,2,3,4,5,4,3,2,1.5,1,0];
ymax = max(y);
y75 = 0.75*ymax;
Method one: contourc, something like this:
>> C = contourc(x,0:1,[y;y],[y75,y75]);
>> x75 = C(1,C(2,:)==0)
x75 =
3.7500 6.2500
>> plot(x,y,'-b',x75,y75,'*r')
Method two: split the data into increasing and decreasing segements, check for intersections, e.g.:
z = find([true,0~=diff(sign(diff(y))),true]);
n = numel(z)-1;
x75 = nan(1,n);
for k = 1:n
v = z(k):z(k+1);
xi = x(v);
yi = y(v);
x75(k) = interp1(yi,xi,y75);
end
plot(x,y,'-b',x75,y75,'*r')
Note that this very simple algorithm has not been tested on edge-cases, e.g. repeated values along the intersect, local minima/maxima on the intersect, scalar input data, etc.

Más respuestas (1)

KSSV
KSSV el 28 de Ag. de 2020
You need to do interpolation frst to get 75% of maximum value.
x = 0:1:11;
y = [0 1 2 3 4 5 4 3 2 1.5 1 0];
%
y75 = 0.75*max(y) ;
% Do interpolation
xi = linspace(min(x),max(x),10^5) ;
yi = interp1(x,y,xi) ;
% get x75 locations
idx = abs(yi-y75)<10^-3;
plot(x,y)
hold on
plot(xi(idx),yi(idx),'*r')

Productos

Versión

R2020a

Etiquetas

Preguntada:

el 27 de Ag. de 2020

Comentada:

el 28 de Ag. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by