MATLAB Answers

Jason
1

Finding the first occurance using interp1

Asked by Jason
on 27 Feb 2018
Latest activity Edited by Matt J
on 27 Feb 2018
Hello. I have some data (red curve) and I'm trying to find the X value at which Y = 0.2.
I have used
Y20 = interp1(Y,X,0.2,'linear')
which works well, but finds the last occurrance.
How can I find the first occurrance (i.e. around x=9)
Thanks

  0 Comments

Sign in to comment.

2 Answers

Answer by Matt J
on 27 Feb 2018
Edited by Matt J
on 27 Feb 2018
 Accepted Answer

Use only the first two data X,Y data points in the interpolation.

  6 Comments

Matt J
on 27 Feb 2018
i=find(Y(1:end-1)>=0.2 & Y(2:end)<=0.2,1);
Y20=interp1(Y(i:i+1),X(i:i+1),0.2);
Torsten
on 27 Feb 2018
It could happen that Y is increasing, couldn't it ?
Matt J
on 27 Feb 2018
Not according to the posted figure, but even if it could, I think the extension is an exercise I'll leave for the OP.

Sign in to comment.


Answer by Sean de Wolski
on 27 Feb 2018
Edited by Sean de Wolski
on 27 Feb 2018

Use cummax and cummin to find the the first set of points that cross 0.2. Then interp just them.
x = 1:10
y = sin(x)
plot(x,y)
yval = 0.2;
idx = find(cummin(y)<0.2 & cummax(y)>0.2, 1, 'first')
interp1(y([idx-1 idx]), x([idx-1, idx]), 0.2)

  1 Comment

Thankyou for your answer. Im sorry I can't accept both. Matt came first.

Sign in to comment.