# Finding the index in two different arrays where values are closest

조회 수: 25(최근 30일)
noMathWiz 2020년 10월 21일
답변: madhan ravi 2020년 10월 21일
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
clc
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);

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

### 답변(2개)

Sulaymon Eshkabilov 2020년 10월 21일
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표시숨기기 이전 댓글 수: -1

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

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

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

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by