Using the find function to check if the numbers in an array are divisible by a given number
조회 수: 94 (최근 30일)
이전 댓글 표시
x = 0:1:10
f = x.^2-x-1
M = find( ~mod(f,2) & ~mod(f,3))
I can use the ~mod(f,2) function to find if the numbers in an array are even but, when applying this to other numbers it returned 'M = 1×0 empty double row vector'. How would this also work when excluding values that are also divisible by another number. For example what numbers in the array are divisible by 3 and 5 but not by 7.
What is the general rule when approaching this?
댓글 수: 0
채택된 답변
DGM
2023년 1월 7일
편집: DGM
2023년 1월 7일
Using find() here is doesn't break anything, but it's not necessary except for one side benefit. Consider the examples:
% integers between 1,100
f = 1:100;
% this are logical arrays and can be used directly without find()
% divisible by 2 and 3
mask = ~mod(f,2) & ~mod(f,3);
f(mask)
% divisible by 10 and not by 20
mask = ~mod(f,10) & mod(f,20);
f(mask)
% divisible by 3 and 5 and not by 7
mask = ~mod(f,3) & ~mod(f,5) & mod(f,7);
f(mask)
% multiple cases can be combined
% not divisible by any of the integers 2:9 (assuming f is a row vector)
mask = all(mod(f,(2:9).'),1);
f(mask)
Note that all of these expressions rely on some operator (all(), ~, or &) causing the numeric output of mod() to be converted to logical. It makes everything concise, but if one isn't careful to make sure this conversion happens, problems can occur.
% not divisible by 2
f = 1:10;
mask = mod(f,2) ~= 0; % this works since the mask is logical
f(mask)
mask = logical(mod(f,2)); % this works since the mask is logical
f(mask)
mask = find(mod(f,2)); % using find() does the same test, but also converts to indexes
f(mask)
mask = mod(f,2); % but this output is still non-integer numeric
f(mask)
In a way, find() does safeguard against this, since it's testing its input for inequality with 0. If you want to safeguard against mistakes allowing this sort of scenario to happen, the simpler way might be to use logical(), as it involves less potentially unnecessary work.
댓글 수: 3
Image Analyst
2023년 1월 7일
Then why did you accept it, and not even try my answer below, which I believe works?
DGM
2023년 1월 7일
x = 0:1:10;
f = x.^2-x-1
None of these few numbers happen to be integer multiples of 2 or 3
m1 = ~mod(f,2)
m2 = ~mod(f,3)
Though the last test is true for all.
m3 = logical(mod(f,7))
So the intersection of those masks is null.
추가 답변 (1개)
Image Analyst
2023년 1월 7일
Try it this way:
x = 0 : 1 : 100;
f = x .^ 2 - x - 1;
% Determine factors so we can see which might work.
for k = 1 : length(f)
if f(k) < 0
continue
end
fprintf('Prime factors of %d are: ', f(k))
fprintf('%d ', factor(f(k)))
fprintf('\n');
end
% mod(f,11)
% mod(f,5)
% Find numbers divisible by both 5 and 11
indexes = find( mod(f, 5) == 0 & mod(f, 11) == 0)
numbers = f(indexes)
% Double check results to make sure they're integers:
numbers/5
numbers/11
% If the above are empty, then there is no pair satisfying the test.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Detection에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!