How to find the point of intersection of two lines in matlab ?
조회 수: 1,379 (최근 30일)
이전 댓글 표시
I have two llines say f1 and f2, each having 100 data points. How to find the point of intersection of these two lines or how to find a points in f1 and f2 which have nearly equal values
댓글 수: 1
Rik
2018년 10월 3일
You should either attach the data, or provide us with some details about what function is fitted to the data.
채택된 답변
KALYAN ACHARJYA
2018년 10월 3일
편집: KALYAN ACHARJYA
2018년 10월 3일
% You can proceed this way also
t=0:.1:10;
y1=sin(t); % say f1
y2=exp(t); %f2
intersection=find(y1==y2);
댓글 수: 5
Benoit Pasquier
2023년 11월 6일
편집: Benoit Pasquier
2023년 11월 6일
@DGM No, the "answer" is incorrect because the question specifically mentions "nearly equal values". Better answers are provided in the comments (e.g., Rik's suggestion) and in other answers.
DGM
2023년 11월 6일
편집: DGM
2023년 11월 6일
Actually, I suppose you have a good point about the specifics.
I tend to lean toward communicating value through upvoting and letting the OP have dibs on making the decision as to what's chosen as "accepted". Other moderators might feel differently, especially on a high-traffic thread. If so, I'll leave that up to them.
Personally, if I were to consider replacing the answer on a high-traffic thread, I would not be strictly concerned with the specific needs of OP anymore. I instead would be looking for a more comprehensive reference answer that might also better serve the varied needs of current and future readers. After all, the question that 1600 people a month expect to be answered is in the title, and it's very generic.
I agree that exact matching is not typically usable, but I also don't think that nearest-neighbor matching in 1D adequately satisfies the generalized needs. The data is often explicitly specified on mismatched x-locations. The pointlists often differ in length. We often want the point where the polyline segments intersect, not just the nearest vertex -- in fact, that's the case suggested by the title.
If you would like to express your preference for one of the existing answers, you can upvote one. If you would like to try your hand at writing a reference answer covering various approaches to various cases, or if you know of other threads which would serve as good references, you're free to do that as well.
For what it's worth, this thread has been sitting in my bookmarks because I know it could probably use more elaboration. I bookmark them, but I'll never get to them all. That said, while I can change which answer is accepted, I can't do so if I'm the one who wrote the reference answer.
추가 답변 (2개)
Preetham Manjunatha
2022년 2월 8일
Here is the link to find the intersection point of two line segments/lines. A fast two line intersection point finder based on the line parametric space. Finds the intersection point between two lines if it exists or else submits NaN. if you need to find the intersection of the multiple line segments, MATLAB's Mapping Toolbox has function polyxpoly - that finds the intersection points for lines or polygon edges.
댓글 수: 0
Clement Hilty
2023년 3월 8일
편집: DGM
2023년 11월 4일
Heyo, if your data isn't a bunch of exactly matching numbers but you still need to find the closest intersection, you can do this:
% Line1 and Line2 are two sets of data based on the same X-indexes
smallNumber = 1
[~,index]=find(abs(Line1-Line2)<smallNumber)
% the closest intersection is:
[X_values(index) Line1(index)] % could also be [X_values Line2(index)]
% The values of Line1 and Line2 never actually equal each other,
% but they get super close.
Then, decrease smallNumber until you only have one response value for "index". For the task I set out to find this for, the smallNumber was actually 1.65 because my data is spaced apart.
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!