Display intersection of two arrays

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

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

Thank you for your help!
I just tested it with my data:
A1= readmatrix('\\myDesktop\stress strain data', ' Range', 'C4:D7058'); %import data from columns C & D to [A1]
A=unique(A1); %get rid of repeats
AA1= readmatrix('\\myDesktop\stress strain data', ' Range', 'E4:F193'); %import data from columns E & F to [AA1]
AA= unique(AA1); %get rid of repeats
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)'
I got this error at Line 7:
Index in position 2 exceeds array bounds (must not exceed 1).
I may interpreting the error incorrectly, but both [A] and [AA] have 2 columns, so I don't understand why the array bounds are being limited to 1.
I believe I see what the problem is, although I would have to have the data to be certain.
Try substituting these —
A = unique(A1, 'stable', 'rows'); %get rid of repeats
and similarly —
AA = unique(AA1, 'stable', 'rows'); %get rid of repeats
The 'stable' argument retains the original ordering, and the 'rows' argument checks unique rows.
Note that the results of —
AAA1 = randi(9, 10, 2)
AAA1 = 10×2
5 9 8 6 3 4 2 6 2 5 9 1 2 2 1 4 7 1 5 7
AAA = unique(AAA1)
AAA = 9×1
1 2 3 4 5 6 7 8 9
AAA = unique(AAA1, 'stable', 'rows')
AAA = 10×2
5 9 8 6 3 4 2 6 2 5 9 1 2 2 1 4 7 1 5 7
are significantly different. Tne unique call without the other arguments creates a single column vector, and that result throws the dimension error.
.
Thank you again for your help!
I rewrote unique() as you suggested:
A1 = readmatrix('\\myDesktop\stress strain data', ' Range', 'C4:D7058'); %import data from columns C & D to [A1]
A = unique(A1, 'stable', 'rows'); %get rid of repeats
AA1 = readmatrix('\\myDesktop\stress strain data', ' Range', 'E4:F193'); %import data from columns E & F to [AA1]
AA = unique(AA1, 'stable', 'rows'); %get rid of repeats
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)'
But I got a new error at line 7:
Error using matlab.internal.math.interp1
Sample points must be unique
I have attached my data in case it will help
Getting the unique values proved to be a bit of a challengt, since the problem was that the first column of ‘ A1’ needs to be unique in order for the code to work. That required getting the index outputs from unique in order to be certain that only the rows with unique values of tthe first column of ‘A1’ were present in ‘A’. After that, the rest was straightforward.
I completed the code as well after that (using the previious code I posted that did not need any alteration to run here), returning the intersection and plotting the result —
A1 = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/841740/stress%20strain%20data.xlsx', 'Range', 'C4:D7058') %import data from columns C & D to [A1]
A1 = 7055×2
0 0.0368 -0.0000 0.0442 -0.0000 0.0768 -0.0000 0.1411 -0.0001 0.2515 -0.0001 0.4436 -0.0000 0.8216 -0.0000 1.4324 -0.0000 2.4657 0.0000 4.0369
[A11,ia,ic] = unique(A1(:,1), 'stable'); %get rid of repeats (in first column) & return row indices
A = A1(ia,:)
A = 6957×2
0 0.0368 -0.0000 0.0442 -0.0000 0.0768 -0.0000 0.1411 -0.0001 0.2515 -0.0001 0.4436 -0.0000 0.8216 -0.0000 1.4324 -0.0000 2.4657 0.0000 4.0369
AA1 = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/841740/stress%20strain%20data.xlsx', 'Range', 'E4:F193') %import data from columns E & F to [AA1]
AA1 = 190×2
0.0020 0 0.0020 -314.2909 0.0020 -314.2909 0.0020 -442.8676 0.0019 -528.5873 0.0019 -700.0274 0.0020 -342.8633 0.0020 -228.5741 0.0020 -42.8572 0.0020 385.7056
AA = unique(AA1, 'stable', 'rows'); %get rid of repeats
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); % Interpolated '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
format long g
xi
xi =
0.00902625838199868
yi
yi =
63236.3254379882
figure
plot(A(:,1), A(:,2))
hold on
plot(AA(:,1), AA(:,2))
plot(xi, yi, 'sg', 'MarkerSize',15, 'LineWidth',2)
hold off
xlim([-0.01 0.05])
legend('A','AA','Intersections', 'Location','best')
text(xi, yi, sprintf(' \\leftarrow Intersection: (%.9f, %9.2f)', xi, yi), 'Horiz','left', 'Vert','middle')
That works!
The necessity to guarantee the uniqueness of the matrices makes this generally applicable, however it may need to be adjusted for other data if they do not conform to these data. The approach used in getting the unique values of ‘A’ here will likely work for ‘AA’ as well, although the unique call with 'stable' and 'rows' worked here.
Note — sorting only the first column only requires the 'stable' argument, not 'rows'.
.
It worked! Thank you so much for your patience and all your help!
As always, my pleasure!
.

Sign in to comment.

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!