How would I write this code without using loops?
2 ビュー (過去 30 日間)
古いコメントを表示
x57a= [];
for i= 1:1000
if rem(i,5) ==0 || rem(i,7) ==0
if rem(i,5)==0 && rem(i,7) ==0
continue;
end
x57a = [x57a, i];
end
end
I wrote this for loop that gives me the multiples of 5 and 7 (but not both) in 1 to 1000, but I am having trouble rewriting it using indexing or even the find function. I just don't want to use loops
1 件のコメント
回答 (2 件)
KSSV
2020 年 2 月 5 日
i = 1:1000 ;
O = zeros(size(i)) ;
idx1 = rem(i,5)==O | rem(i,7)==O ;
idx2 = rem(i,5)==O & rem(i,7)==O ;
iwant = find(idx1 ~= idx2) ;
0 件のコメント
Alex Mcaulley
2020 年 2 月 5 日
Another option:
N = 1000;
five = 2*5:5:N;
seven = 2*7:7:N;
res = setxor(five,seven)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Polynomials についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!