Determining the intersection of two vectors

Below is my working code for a simple program to graph two vectors.
line([0,50],[30,30]); line1 = [0 30 50 30 ];
line([55,55],[15,30]); line2 = [55 15 55 30];
C = intersect(line1,line2)
Below is the visual representation of these two lines:
It is barely visible in the top right corner that these two lines do not intersect. intersect(A,B) however indicates they're intersecting at 30. I assume this is because it treats the vectors as lines which extend forever? Is there any functionality for matlab which would handle only the given space as opposed to infinite length? It should ideally return these lines do not intersect.
Thanks!

 채택된 답변

Star Strider
Star Strider 2014년 12월 19일

2 개 추천

The intersect function is a set operation in MATLAB. It looks for the points (elements) that ‘line1’ and ‘line2’ have in common. They both have 30 so that is the correct result.

댓글 수: 3

Star Strider
Star Strider 2014년 12월 19일
편집: Star Strider 2014년 12월 19일
‘Ah, ok thanks. Is there a different function for (x,y) intersection point which returns NaN if they don't intersect? That's really what I'm looking to find.’
The only thing I can think of is ... intersect ... but with the restriction that you have to use matrix representations of your x and y data (with x in the first column and y in the second in my example here) and then use the 'rows' option.
Using the coordinates you plotted in your line calls:
L1 = [ 0 50; 30 30]';
L2 = [55 55; 15 30]';
Li = intersect(L1, L2, 'rows');
The result for ‘Li’ is the empty matrix, not NaN, but you can fix that with:
Li(isempty(Li)) = NaN;
to force ‘Li’ to be NaN if the result is an empty matrix.
Jack
Jack 2014년 12월 20일
That's perfect, thanks.
Star Strider
Star Strider 2014년 12월 20일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 19일

1 개 추천

line1 and line2 are two arrays,
C = intersect(line1,line2)
will return the common elements to the two arrays
Jack
Jack 2014년 12월 19일

0 개 추천

Ah, ok thanks. Is there a different function for (x,y) intersection point which returns NaN if they don't intersect? That's really what I'm looking to find.
Mouna SAMAALI
Mouna SAMAALI 2021년 5월 12일

0 개 추천

I have a question about how can I convert this Line of code matlab to Simulink . I would like to use only blocks Simulink ?
A=union(A,setxor(B,intersect(B,C)));where A, B et C are vectors
So I ask about blocks Simulink that make the intersection, the union and setxor of two vectors.
Thanks in advance for your help .

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

질문:

2014년 12월 19일

답변:

2021년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by