using the find function to find intersection of two lines
2 views (last 30 days)
Show older comments
shobhit mehrotra on 15 Apr 2015
Commented: Renato Agurto on 16 Apr 2015
I have a vector A :
A = [ 10 20 30 40 50 60 50 40 30 20 10]
I want to find the nearest index where it crosses 18, so in this case it would be 2 and 10.
AA = find(A=18)
you are looking for sign changes of A-18:
find((A(2:end)-18).*(A(1:end-1)-18) < 0)
Hope this helps,
Renato Agurto on 16 Apr 2015
I just want to complement the answer, so the right index is shown if is nearer as the left:
tmp = find((A(2:end)-18).*(A(1:end-1)-18) < 0)
B = tmp + (abs(A(tmp) - 18) > abs(A(tmp+1)-18))
More Answers (1)
Adam on 15 Apr 2015
find( A == 18 + min( abs( A - 18 ) ) )
is a one-liner to do what you want. Personally I would probably do it as multiple lines, but the idea is the same.
That's better than mine, because my code always selects the point to the left, Adam's picks the one that's nearer ...
Find more on Logical 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!