Finding the index in two different arrays where values are closest

62 views (last 30 days)
I have two arrays. I want to find the index in each of the arrays where each of them is closest.
In the below plot of two arrays, array a is closed to array b at a(8) and b(9) because a(8) and b(9) have the closest y-values to each other than any other two independent y values.
Another way of wording the question: At which index do the y-values of each array intersect? If there are no intersections, what is the y-value where they both come closest to, and what is the index position in each array for that y-value?
clear all
close all
R1 = 12964;
R2 = 34447.2;
PRF1 = 4000;
PRF2 = 3500
c = 3e8;
n = 0:100;
Rtrue1 = R1 + n.*(c/(2*PRF1));
Rtrue2 = R2 + n.*(c/(2*PRF2));
plot(Rtrue1,'b.', 'MarkerSize',8);
hold on
plot(Rtrue2,'r.', 'MarkerSize',8);

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 21 Oct 2020
The answer is a quite straightforward that is to find the minimum of differences between the two lines. E.g.:
DR = Rtrue1-Rtrue2;
Ans = min(DR);
IND = DR==Ans;
Rtrue1(IND) % Solution
Rtrue2(IND) % Solution

madhan ravi
madhan ravi on 21 Oct 2020
Note: Not tested.
ix = abs(a(:) - b(:).');
[~, ID] = min(ix(:));
[where_a, where_b] = ind2sub(size(ix), id)





Community Treasure Hunt

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

Start Hunting!

Translated by