Display intersection of two arrays
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sabrina Fleming
le 16 Déc 2021
Commenté : Star Strider
le 6 Jan 2022
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 commentaires
Réponse acceptée
Star Strider
le 16 Déc 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
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 commentaires
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!