フィルターのクリア

How do you remove multiples of certain numbers from a row vector?

13 ビュー (過去 30 日間)
Caroline F
Caroline F 2022 年 3 月 7 日
コメント済み: Stephen23 2022 年 3 月 16 日
I am trying to create a for loop that finds all values in the range [1,100] that are also multiple of 3 or 5, but not 3 AND 5, and save these in a row vector labeled M1. It was suggested that I use mod()/rem() functions. I am struggling on how to remove the numbers that are multiples of 3 and 5.
M1=[];
for x = 1:100
if mod(x,3)==0||mod(x,5)==0
M1=[M1,x];
end
if mod(x,3)==0 & mod(x,5)==0
M1(x)=[]
end
end
M1

採用された回答

Max Alger-Meyer
Max Alger-Meyer 2022 年 3 月 7 日
The easiest way to do this is with is just adding additional conditions to your first if statement, and deleting the second if statement alltogether. We do this by placing the "or" conditions in parantheses, and having a third condition that checks that the sum of the mod(x,3) and mod(x,5) isn't zero. If mod(x,3) and mod(x,5) sum to zero, then we know that the index must be wholly divisible by both.
M1=[];
for x = 1:100
if (mod(x,3) == 0||mod(x,5) == 0) && sum(mod(x,3) + mod(x,5)) ~= 0
M1=[M1,x];
end
end
M1
M1 = 1×41
3 5 6 9 10 12 18 20 21 24 25 27 33 35 36 39 40 42 48 50 51 54 55 57 63 65 66 69 70 72
  2 件のコメント
Garrett Viton
Garrett Viton 2022 年 3 月 14 日
How would one go about doing this if it was "3 and 5" instead of "3 or 5"?
Max Alger-Meyer
Max Alger-Meyer 2022 年 3 月 15 日
So if I understand you correctly, you want to remove only numbers that are divisible by both 3 and 5? If that's the case, you could just use the second condition in the if statement:
M1=[];
for x = 1:100
if sum(mod(x,3) + mod(x,5)) ~= 0
M1=[M1,x];
end
end
M1
M1 = 1×94
1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 20 21 22 23 24 25 26 27 28 29 31 32
Alternatively, we can simplify this a little further by thinking about what is really happening. This didn't cross my mind the first time, but if you looks at the outputs from my original answer, you can see that the only numbers that are divisible by both 3 and 5 are just going to be multiple of 15, so we can use mod(x,15) instead of checking the sum of the mods of 3 and 5. That leaves us with:
M1=[];
for x = 1:100
if mod(x,15) ~= 0
M1=[M1,x];
end
end
M1
M1 = 1×94
1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 20 21 22 23 24 25 26 27 28 29 31 32

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2022 年 3 月 15 日
編集済み: Jan 2022 年 3 月 16 日
You do not need a loop.
n = 100;
x = false(1, n);
x(3:3:n) = true;
x(5:5:n) = true;
x(15:15:n) = false;
Result = find(x)
Result = 1×41
3 5 6 9 10 12 18 20 21 24 25 27 33 35 36 39 40 42 48 50 51 54 55 57 63 65 66 69 70 72
% Or:
x = 1:100;
Result = x((mod(x, 3) == 0 | mod(x, 5) == 0) & mod(x, 15) ~= 0)
Result = 1×41
3 5 6 9 10 12 18 20 21 24 25 27 33 35 36 39 40 42 48 50 51 54 55 57 63 65 66 69 70 72
% Shorter:
Result = x(~(mod(x, 3) & mod(x, 5)) & mod(x, 15))
Result = 1×41
3 5 6 9 10 12 18 20 21 24 25 27 33 35 36 39 40 42 48 50 51 54 55 57 63 65 66 69 70 72

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by