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
1 件のコメント
Stephen23
2018 年 12 月 28 日
回答 (2 件)
madhan ravi
2018 年 12 月 28 日
編集済み: madhan ravi
2018 年 12 月 28 日
You may need to clarify by giving a certain example because the answer is assumed that A and B has same number of elements.
nnz((abs(B(:)-A(:))<C)) % no loops needed , nnz counts how many satisfy the condition
7 件のコメント
Stephen23
2018 年 12 月 28 日
@madhan ravi: how will this work if A and B have different lengths (as the question stated)?:
B(:)-A(:)
Yeltepe
2018 年 12 月 28 日
madhan ravi
2018 年 12 月 28 日
編集済み: madhan ravi
2018 年 12 月 28 日
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)
Yeltepe
2019 年 1 月 2 日
madhan ravi
2019 年 1 月 2 日
Anytime :) ,if it helped you make sure to accept the answer.
Yeltepe
2019 年 1 月 2 日
Yeltepe
2019 年 1 月 2 日
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!