Finding the first occurance using interp1
4 views (last 30 days)
Show older comments
Hello. I have some data (red curve) and I'm trying to find the X value at which Y = 0.2.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/170812/image.png)
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
Accepted Answer
More Answers (1)
Sean de Wolski
on 27 Feb 2018
Edited: 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)
See Also
Categories
Find more on Multirate Signal Processing 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!