intersect() returns unplausible values

3 views (last 30 days)
Michael Me
Michael Me on 23 Nov 2021
Commented: Jon on 24 Nov 2021
I am trying to return the coordinates of the intersection of two lines (please see below plot). I need the x- and y-values of the point of intersection. Code looks like this:
%Arbeitspunkt Daten
%Mm = blue line
%Mld = red dashed line
if Mld > 0
[M_ap, s_ap] = intersect(Mm, Mld);
app.LabelM_ap.Visible = 'on';
app.LabelM_ap.Text = sprintf('%.1f', M_ap);
else
app.LabelM_ap.Visible = 'off';
end
However, I set a breakpoint at the last "end" and reviewed the workspace variables M_ap and s_ap. For those two variables I do not get any values written back from intersect() function, even tho there is a intersection between these two lines at roughly ~95%
Mm and Mld are both 1x100000 double.
Does intersect()-function have trouble processing double format? Is there a way to integrate this differently?
Thanks in advance!

Accepted Answer

Jon
Jon on 23 Nov 2021
I think you may be misunderstanding the usage of MATLAB's intersect function. It finds the set intersection (elements in common) between two collections of values. It looks like you may be looking for the intersection between two piecewise linear functions, specified by your data vectors Mm and Mld along with whatever the corresponding "x" values that go along with these vectors.
In general you would have to test to see each segment of Mm to see if it crosses (geometrically intersects) any segment of Mld, and if so where the line segments have the same value (the intersection point). I haven't tried it but it looks like this recent File Exchange entry might do what you are looking for: https://www.mathworks.com/matlabcentral/fileexchange/26014-finding-zeros-and-intersections by @Patrick might do what you are looking for.
Note in general you should cover the possibility that their are no intersections or more than one.
  3 Comments
Michael Me
Michael Me on 24 Nov 2021
Thanks! I should read the doc more. While your suggestion of the recent file exchange didn't work, I did some more research and found the function InterX (https://de.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections?requestedDomain=) which worked pretty well.
Jon
Jon on 24 Nov 2021
I'm glad you got things working. Sorry my File Exchange suggestion didn't work, I hadn't tried it out, but the description sounded promising. Good though that you were able to find another one that worked.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 23 Nov 2021
That's not what intersect does.
C = intersect(A,B) returns the data common to both A and B, with no repetitions. C is in sorted order.
If you want to find where two curves cross you'll have to use a different method. And it depends if you want to do it numerically (nearest actual index) or analytically (exact, but interpolated, index location)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!