How to use a counter with a loop

조회 수: 175 (최근 30일)
Jake
Jake 2013년 5월 6일
답변: Sakhumzi 2024년 5월 3일
How do you use a counter with a for loop?
I want to learn how to input a bunch of numbers into a loop and use the counter to find how many "count numbers" I get for that input. Whichever input gets the largest "count number" I want to save that input and display it.

답변 (3개)

Youssef  Khmou
Youssef Khmou 2013년 5월 6일
편집: Youssef Khmou 2013년 5월 6일
hi,
here is fast way :
N=1:1000;
P=isprime(N);
Prime=find(P==1);
  댓글 수: 2
Jake
Jake 2013년 5월 6일
Thank you, but that's not what I'm looking for. I want he program to take an "x" value, that is, from 1:999. For example, 132. I want the program to divide 132 by c ( 132/2, 132/3, 132/5...) If there are no remainders after division for each of these numbers, 1 is added to the counter. The number x that produces the greatest count number is the value that I want to keep. I hope this is clear.
Youssef  Khmou
Youssef Khmou 2013년 5월 6일
편집: Youssef Khmou 2013년 5월 6일
hi, according to this description here is the translation :
N=1000;
c=[2 3 5 7 11 13];
counter=0;
ctr=1;
for x=1:N
if sum(rem(x,c))==0
counter=counter+1;
P(ctr)=x;
ctr=ctr+1;
else
continue;
end
end
But logically that s not right . counter is 0 .

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


Youssef  Khmou
Youssef Khmou 2013년 5월 6일
편집: Youssef Khmou 2013년 5월 6일
hi,
Second way :
K=primes(1000);
Third way :
N=1000;
c=[2 3 5 7];
counter=1;
for x=1:N
V=mod(x,c);
F=V(V==0);
if isempty(F)
Prime(counter)=x;
counter=counter+1;
else
continue;
end
end

Sakhumzi
Sakhumzi 2024년 5월 3일
N=1000;
c=[2 3 5 7];
counter=1;
for x=1:N
V=mod(x,c);
F=V(V==0);
if isempty(F)
Prime(counter)=x;
counter=counter+1;
else
continue;
end
end

카테고리

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