Irregular intervals inside a for loop

조회 수: 2 (최근 30일)
Kerr Beeldens
Kerr Beeldens 2022년 3월 23일
편집: John D'Errico 2022년 3월 23일
Hello, I'm trying to work out how to do two actions (lets call them A and B), where action A is repeated n amount of times after which action B is repeated m amount of times, with n and m being integers.
this all takes place in a for loop with say, index p.
Essentially what I want as my output when n = 2 and m = 3 is:
Action A
Action A
Action B
Action B
Action B
Action A
Action A
...etc.
I tried doing the following:
n=5
for p = 1:100
q = floor(p/n);
if rem(q,2) == 0
disp('Action A')
else
disp('Action B')
end
end
But ofcourse, this only works for regular intervals (I'm quite a nooby in MATLAB so excuse me if this is a weird way to approach this)

답변 (3개)

Voss
Voss 2022년 3월 23일
n = 2;
m = 3;
z = n+m;
for p = 1:20
if ismember(mod(p,z),1:n)
disp('Action A');
else
disp('Action B');
end
end
Action A Action A
Action B Action B Action B
Action A Action A
Action B Action B Action B
Action A Action A
Action B Action B Action B
Action A Action A
Action B Action B Action B
  댓글 수: 4
Kerr Beeldens
Kerr Beeldens 2022년 3월 23일
Thanks for your words of caution. This was exactly what I was refering to in my other comment, where I mentioned that the use of for loops inside the for loop may be undesirable in my code. I will play around with both to see which method I favour :)
Voss
Voss 2022년 3월 23일
Whatever works works! I just wanted you to be aware of the difference.

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


David Hill
David Hill 2022년 3월 23일
for p=1:100
for a=1:n
%Action A
end
for b=1:m
%Action B
end
end
  댓글 수: 3
Image Analyst
Image Analyst 2022년 3월 23일
@Kerr Beeldens it does work nicely. And it does what you said in a simpler and more straightforward method than messing with floor() and rem() like you tried to do.
Kerr Beeldens
Kerr Beeldens 2022년 3월 23일
Thanks for the reply, I figured out how to use this in my code. Both suggested methods work well, I prefer this one for visual simplicity.

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


John D'Errico
John D'Errico 2022년 3월 23일
편집: John D'Errico 2022년 3월 23일
There are multiple ways to do this. For example:
P = primes(20); % Note that P is a ROW vector
P
P = 1×8
2 3 5 7 11 13 17 19
SumP = 0;
for P_i = P
SumP = SumP + P_i;
end
SumP
SumP = 77
Do you see there is effectively no index at all? The loop variable is the corresponding element of P.
Or, I might have done this:
Q = (1:5).^2
Q = 1×5
1 4 9 16 25
ProdQ = 1;
for ind = 1:numel(Q)
Q_ind = Q(ind);
ProdQ = ProdQ*Q_ind;
end
ProdQ
ProdQ = 14400
In the latter case, I used a uniform index in the form of ind, but then immediately index into the non-uniform vector Q.
Next, we can have a loop on characters.
S = 'The Quick Brown Fox jumped over the lazy dog';
words = split(S).'
words = 1×9 cell array
{'The'} {'Quick'} {'Brown'} {'Fox'} {'jumped'} {'over'} {'the'} {'lazy'} {'dog'}
Again, note that words is again a ROW vector. Now the for loop can access the elements of this row vector.
for W = words
disp(W{:})
end
The Quick Brown Fox jumped over the lazy dog
Be careful, as that trick fails when the vector is a column vector.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by