finding similar elements in two arrays

Hello everyone,
I have two very long arrays with different lengths and would like to find common elements in them within a certain limit (C below).
The code below finds the elements up to the first different value in B. I need all coincident data.
Thank you in advance.
C=2;
g=1;
for j=1:length(A)
if (abs(B(g)-A(j))<C)
g=g+1;
end
end

Answers (2)

madhan ravi
madhan ravi on 28 Dec 2018
Edited: madhan ravi on 28 Dec 2018
You may need to clarify by giving a certain example because the answer is assumed that A and B has same number of elements.
You may want to use uniquetol() or ismembertol() lookup.
nnz((abs(B(:)-A(:))<C)) % no loops needed , nnz counts how many satisfy the condition

7 Comments

@madhan ravi: how will this work if A and B have different lengths (as the question stated)?:
B(:)-A(:)
The following is part of the data I am trying to find similar items in the arrays:
A:
2291,89200000000
3653,05800000000
5431,06800000000
7597,12800000000
11301,7080000000
14637,9060000000
15815,2940000000
20264,9300000000
22313,6540000000
22913,3660000000
24473,5040000000
25041,9700000000
25972,3800000000
26618,3100000000
29317,4920000000
31036,7780000000
31837,3060000000
34589,4680000000
36750,3420000000
41106,1940000000
50789,1380000000
51842,1160000000
52978,1980000000
54409,5060000000
57256,6080000000
59142,1880000000
59742,6400000000
61236,9140000000
61819,3500000000
62550,7180000000
63170,9640000000
64403,8160000000
66923,6360000000
68480,3560000000
69181,4300000000
75626,5280000000
77726,4260000000
80912,6360000000
B:
2291,89948000000
3653,06148000000
5431,06548000000
6574,11348000000
7597,13148000000
11301,7054800000
14637,8894800000
15815,2914800000
16805,1254800000
19720,9934800000
20264,9294800000
22313,6514800000
22913,3694800000
24473,4994800000
25041,9754800000
25972,3734800000
26618,3054800000
29317,4894800000
30382,9894800000
31036,7754800000
31837,3094800000
34589,4734800000
36750,3374800000
40045,2754800000
41106,1954800000
49804,8974800000
50789,1434800000
51842,1174800000
52978,1974800000
54359,0354800000
57256,6134800000
57767,8474800000
58554,5174800000
59142,1874800000
59742,6394800000
61236,9154800000
61819,3554800000
62550,7174800000
63170,9634800000
My tolerance is 0.5.
Note: I believe the commas in your A and B should be dots meaning the float numbers , the comma makes it as two columns
idx=ismembertol(A,B,0.5)
% or perhaps
uniquetol([A;B],0.5)
The commas are dots but when I am trying to paste it, it copies as commas. Anyways, I will try your suggestion. Thanks.
Anytime :) ,if it helped you make sure to accept the answer.
new values with dots
a:
2291.89948000000
3653.06148000000
5431.06548000000
6574.11348000000
7597.13148000000
11301.7054800000
14637.8894800000
15815.2914800000
16805.1254800000
19720.9934800000
20264.9294800000
22313.6514800000
22913.3694800000
24473.4994800000
25041.9754800000
25972.3734800000
26618.3054800000
29317.4894800000
30382.9894800000
31036.7754800000
31837.3094800000
34589.4734800000
36750.3374800000
40045.2754800000
41106.1954800000
49804.8974800000
50789.1434800000
51842.1174800000
52978.1974800000
54359.0354800000
57256.6134800000
57767.8474800000
58554.5174800000
59142.1874800000
59742.6394800000
b:
2291.89200000000
3653.05800000000
5431.06800000000
7597.12800000000
11301.7080000000
14637.9060000000
15815.2940000000
20264.9300000000
22313.6540000000
22913.3660000000
24473.5040000000
25041.9700000000
25972.3800000000
26618.3100000000
29317.4920000000
31036.7780000000
31837.3060000000
34589.4680000000
36750.3420000000
41106.1940000000
50789.1380000000
51842.1160000000
52978.1980000000
54409.5060000000
57256.6080000000
59142.1880000000
tolerance:
0.5
uniquetol and ismembertol did not work. sorry.

Sign in to comment.

Yeltepe
Yeltepe on 2 Jan 2019
by the way the following code works but takes some time for large arrays and I need a quicker version:
for i=1:size(B)
for j=i:size(A)
if abs(A(j)-B(i))<=tolerance
D(i)=B(i);
break
end
end
end

Products

Release

R2016b

Asked:

on 28 Dec 2018

Answered:

on 2 Jan 2019

Community Treasure Hunt

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

Start Hunting!