1

In MATLAB (or more generally) if I wanted something to happen only, for example, every 50 iterations of a for loop how could I do this better than below? That is to not write out every possible value of +50. Thanks.

for i = 1:1060;
    if i = 50 || 100 || 150 || ... || 1050
        randi(i); % for example, just do something
    end;
end;
1
  • 2
    I don't know the MATLAB syntax, but you are looking for the modulus (remainder after division) function: E.g. if i mod 50 = 0 .. Commented Dec 6, 2012 at 4:24

2 Answers 2

6

What you want is

if mod(i, 50) == 0
  do something
Sign up to request clarification or add additional context in comments.

1 Comment

I changed your answer (+1) to Matlab syntax (since it is a Matlab question), hope that's okay. Cheers.
5

What you want is

for i = 0:50:1050
    do_stuff(i);
end

unless, it is unclear from your question, if the previous answer is what you really want, which you might need in the case that looks like this

for i = 1:1060
    if mod(i, 50) == 0
        do_something(i)
    end
    do_something_else(i)
end

Cheers,--

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.