필터 지우기
필터 지우기

For loop and omitting every 10th step

조회 수: 6 (최근 30일)
Panty
Panty 2015년 1월 20일
댓글: Panty 2015년 1월 21일
Hello,
I am doing some Bayesian by running a Gibbs sampler. My issue is that I would like to omit (drop) the draws I get in every 10th repetition. So I was wondering whether there is any command or trick while I am using the FOR command, to ignore or jump the every 10th repetition. So for example:
for i=1:30
I would i to take values 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 28 29
Any idea? Many thanks
  댓글 수: 1
Stephen23
Stephen23 2015년 1월 20일
You should not use i or j as the loop variable name, as these are the names of the inbuilt imaginary unit .

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

채택된 답변

Image Analyst
Image Analyst 2015년 1월 20일
for i = 1 : 30
if rem(i, 10) == 0
continue;
end
end
  댓글 수: 6
Panty
Panty 2015년 1월 20일
So if I do it like:
for i = 1 : 30
if rem(i, 10) == 0
continue;
end
for p=1:10
....
end
k=k+1
end
So if i=10,20 or 30 then neither "for p=1:10" nor "k=k+1" will execute. Correct?
Image Analyst
Image Analyst 2015년 1월 20일
Correct.

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

추가 답변 (2개)

David Young
David Young 2015년 1월 20일
for i = floor(1:10/9:30)
...
end
  댓글 수: 2
Panty
Panty 2015년 1월 20일
Thank you very much David for the help. It seems clever as well.
Stephen23
Stephen23 2015년 1월 20일
편집: Stephen23 2015년 1월 20일
Do not use i as the loop variable name, as this is the name of the inbuilt imaginary unit .

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


Stephen23
Stephen23 2015년 1월 20일
A = 1:30;
A(10:10:end) = [];
for a = A
....
end
  댓글 수: 1
Panty
Panty 2015년 1월 21일
Thank you Stephen. It looks helpful as well.

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

카테고리

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