Display intersection of two arrays

6 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 16 Dec 2021
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
xi = 1×2
1.0000 62.4529
yi
yi = 1×2
1.0000 5.2265
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 Comments
Sabrina Fleming
Sabrina Fleming on 5 Jan 2022
It worked! Thank you so much for your patience and all your help!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 16 Dec 2021

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!