How would I write this code without using loops?

조회 수: 4 (최근 30일)
Susana Salinas
Susana Salinas 2020년 2월 5일
답변: Alex Mcaulley 2020년 2월 5일
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
Hiro Yoshino
Hiro Yoshino 2020년 2월 5일
I don't get what you want to do.
Can you elaborate on it a bit more?

댓글을 달려면 로그인하십시오.

답변 (2개)

KSSV
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) ;

Alex Mcaulley
Alex Mcaulley 2020년 2월 5일
Another option:
N = 1000;
five = 2*5:5:N;
seven = 2*7:7:N;
res = setxor(five,seven)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by