Check if values in array fall within certain integer of values in another array

6 ビュー (過去 30 日間)
g
g 2019 年 10 月 26 日
コメント済み: David Hill 2019 年 10 月 26 日
Let's say I have a set of numbers in array A
11
22
35
41
and another set of numbers in array B
3
11
45
I want to check whether the numbers in A are within a certain number (say, 10) of any numbers in array B. 11 fits this criterion, since it is within 10 of both 3 and 11. 35 fits this criterion because it is within 10 of 45, likewise for 41. 22 is not within 10 of any number in B, so 22 would not meet this criterion.
How could I do this with a loop, for any lengths of A and B? (B could also be bigger than A, for example). I essentially want to loop over the values in A, checking if each one is within 10 of any value in B? (If so, then I have a command I would like to perform within the loop with each respective number in A)

採用された回答

Star Strider
Star Strider 2019 年 10 月 26 日
The ismembertol funciton is also an option:
A = [11
22
35
41];
B = [ 3
11
45];
[LA,LB] = ismembertol(A, B, 10, 'DataScale',1);
MeetsCriteria = A(LA)
DoesNotMeetCriteria = A(~LA)
producing:
MeetsCriteria =
11
35
41
DoesNotMeetCriteria =
22
Experiment to get the result you want.

その他の回答 (1 件)

David Hill
David Hill 2019 年 10 月 26 日
C lists all elements of A within +-10 of any value of B.
C=A(find(arrayfun(@(x)sum(abs(B-x)<=10),A)));

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by