Display intersection of two arrays
6 次查看(过去 30 天)
显示 更早的评论
I imported data from excel using 'readmatrix()'.
I got matrix A (7054 x 2) and matrix AA (189x2).
I plotted both matrices on the same graph and saw that they intersect around (0.009, 63048).
They do not intersect at or near (0,0) but are very very close.

I would like to display the exact intersection point (or a very close approximation) in the command window.
I've already tried:
A1= intersect(A,AA) and got ans=0
A2=intersect(A,AA,'rows') and got and= 0x2 empty double matrix
I am relatively new to MATLAB so any help is appreciated
0 个评论
采纳的回答
Star Strider
2021-12-16
It would help to have the data.
Synthesising some, this illustrates a straightforward approach —
A = [(0:7053).' (0:7053).^0.4.']; % Create Matrix
AA = [1+(0:188).' 1+(0:188).^0.35.']; % Create Matrix
xv = linspace(min(AA(:,1)), max(AA(:,1))); % Common Interpoation Vector
A2i = interp1(A(:,1), A(:,2), xv); % Interppolated 'A(:,2)'
AA2i = interp1(AA(:,1), AA(:,2), xv); % Interppolated 'AA(:,2)'
idx = find(diff(sign(A2i-AA2i))); % Approximate Index Of Intersection
for k = 1:numel(idx)
idxrng = max(1, idx(k)-2) : min(numel(xv),idx(k)+2); % Restrict Indices To Region Of Intersections
xi(k) = interp1(A2i(idxrng)-AA2i(idxrng), xv(idxrng), 0); % Interpolate To Find X-Intersection
yi(k) = interp1(xv(idxrng), AA2i(idxrng), xi(k)); % Interpolate To Find Y-Intersection
end
xi
yi
figure
plot(A(:,1), A(:,2))
hold on
plot(AA(:,1), AA(:,2))
plot(xi, yi, 'sg', 'MarkerSize',15, 'LineWidth',2)
hold off
axis([0 500 0 10])
legend('A','AA','Intersections', 'Location','best')
A similar approach should work with the available data. The code here may have to be tweaked a bit to accommodate it.
.
6 个评论
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!