How to find intersection between two array line plots
32 次查看(过去 30 天)
显示 更早的评论
The problem is the array is to plot a payload diagram so the array is made up of 4 x and y points. So basically 4 x,y coordinates but the lines intersect and I want to know at what point they intersect.
The first array has 3 lines horizontal then diagonal then diagonal again but steeper. And the other array has only 2 x,y coordinates and intersects the first diagnonal line! Can you help me find the value at that intersection AKA teh coordinates there.
0 个评论
回答(1 个)
Star Strider
2022-4-5
编辑:Star Strider
2022-4-6
It would help to have the actual data.
x = 1:4;
y1 = rand(size(x));
y2 = rand(size(x));
xi = linspace(min(x), max(x), numel(x)*10); % Increase 'x' Resolution
y1i = interp1(x,y1,xi); % Increase 'y1' Resolution
y2i = interp1(x,y2,xi); % Increase 'y2' Resolution
zxi = find(diff(sign(y1i-y2i))); % Appriximate Indices Of Intersections
for k = 1:numel(zxi)
idxrng = max(1,zxi(k)-1) : min(numel(xi),zxi(k)+1); % Index Range For Interpolation
xix(k,:) = interp1(y1i(idxrng)-y2i(idxrng),xi(idxrng),0); % 'x' Value At Intersection
yix(k,:) = interp1(xi(idxrng),y1i(idxrng),xix(k)); % 'y' Value At Intersection
end
Intersections = table(xix,yix)
figure
plot(x, y1, x, y2)
hold on
plot(xix, yix, 'sm')
hold off
grid
This will be reliable if there is only one intersection between each pair of indices. It may fail to discriminate intersections that are close together, even with the increased point resolution.
EDIT — (6 Apr 2022 at 13:00)
Made ‘xi’ length a function of the original ‘x’ length.
.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
