Clear Filters
Clear Filters

Using the find function to check if the numbers in an array are divisible by a given number

23 views (last 30 days)
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?

Accepted Answer

DGM
DGM on 7 Jan 2023
Edited: DGM on 7 Jan 2023
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)
ans = 1×16
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
% divisible by 10 and not by 20
mask = ~mod(f,10) & mod(f,20);
f(mask)
ans = 1×5
10 30 50 70 90
% divisible by 3 and 5 and not by 7
mask = ~mod(f,3) & ~mod(f,5) & mod(f,7);
f(mask)
ans = 1×6
15 30 45 60 75 90
% 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)
ans = 1×22
1 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
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)
ans = 1×5
1 3 5 7 9
mask = logical(mod(f,2)); % this works since the mask is logical
f(mask)
ans = 1×5
1 3 5 7 9
mask = find(mod(f,2)); % using find() does the same test, but also converts to indexes
f(mask)
ans = 1×5
1 3 5 7 9
mask = mod(f,2); % but this output is still non-integer numeric
f(mask)
Array indices must be positive integers or logical values.
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 Comments
DGM
DGM on 7 Jan 2023
x = 0:1:10;
f = x.^2-x-1
f = 1×11
-1 -1 1 5 11 19 29 41 55 71 89
None of these few numbers happen to be integer multiples of 2 or 3
m1 = ~mod(f,2)
m1 = 1×11 logical array
0 0 0 0 0 0 0 0 0 0 0
m2 = ~mod(f,3)
m2 = 1×11 logical array
0 0 0 0 0 0 0 0 0 0 0
Though the last test is true for all.
m3 = logical(mod(f,7))
m3 = 1×11 logical array
1 1 1 1 1 1 1 1 1 1 1
So the intersection of those masks is null.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 7 Jan 2023
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.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!