MATLAB Answers

Finding the index in two different arrays where values are closest

조회 수: 2(최근 30일)
noMathWiz 21 Oct 2020
답변: madhan ravi 21 Oct 2020
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);

  댓글 수: 0

댓글을 달려면 로그인하십시오.


Sulaymon Eshkabilov
Sulaymon Eshkabilov 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

  댓글 수: 0

댓글을 달려면 로그인하십시오.

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

  댓글 수: 0

댓글을 달려면 로그인하십시오.





Community Treasure Hunt

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

Start Hunting!

Translated by