# Compare two array values that meet criteria

2 views (last 30 days)
ALDO on 10 Sep 2020
Commented: Adam Danz on 10 Sep 2020
Hi
I have two arrays. I know that location of each array value.
the locations are as follows
location Array A= 1253 1982 50616 50665 61911 61999 62560 68112 68162 68376 73573 94747 99547 104436 104680 107002 .... (213 elements)
location Array B=1240 1953 4498 47993 51165 52541 52709 53983 54187 59203 59618 59829 61134 62035 64844 72766 72887 ....(130 elements)
what i would like to know is location value in A matchs B +-15
for example take the first value in A (1253) compare it to all the values in B if you find a value in B that is within the range (1253-15):(1253+15) take both location values and put it in an array (n,2). colum 1 value of A, column 2 value of B.
Thanks you for your help!

Adam Danz on 10 Sep 2020
Edited: Adam Danz on 10 Sep 2020
Your example only contains 1 close-match of values between A and B that are +/- 15 units apart.
% Raw data; A and B can be any length and do not have equal matches.
A = [ 1253 1982 50616 50665 61911 61999 62560 68112 68162 68376 73573 94747 99547 104436 104680 107002];
B = [1240 1953 4498 47993 51165 52541 52709 53983 54187 59203 59618 59829 61134 62035 64844 72766 72887];
% Distance matrix
% d(i,j) is the distance between A(i) and B(j)
d = abs(A(:) - B(:).');
% Find close-matches
[Aidx, Bidx] = find(d<=15);
% Produce outputs
% z is nx2 containing the n close-match values from A and B
% zIdx is nx2 containing the indices of A and B that were matched
% so, [A(zIdx(i,1)), B(zIdx(i,2))] == z(i,:)
z = [A(Aidx); B(Bidx)]';
zIdx = [Aidx, Bidx];
##### 2 CommentsShowHide 1 older comment
Adam Danz on 10 Sep 2020
Glad I could help!