For loop and omitting every 10th step

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 日

0 投票

for i = 1 : 30
if rem(i, 10) == 0
continue;
end
end

6 件のコメント

Image Analyst
Image Analyst 2015 年 1 月 20 日
Yet another way:
indexesToUse = [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]
for i = indexesToUse
fprintf('i = %d\n', i);
end
Panty
Panty 2015 年 1 月 20 日
Clever answer..Thanks you very much 'Image'
Panty
Panty 2015 年 1 月 20 日
Image, Inside my initial 'for' command I have other loops and calculations. How should I write the above trick, so that any time i=10,20,30 everything that is inside my 'for' loop be ignored.
Image Analyst
Image Analyst 2015 年 1 月 20 日
Either of those methods will work. The first one goes through all i but then skips to the end if it hits 10, 20, 30. Put your code after the if block. The second code never even takes on the values 10, 20, 30 so there is no problem, just put your code inside the loop.
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 日

1 投票

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 日

0 投票

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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2015 年 1 月 20 日

コメント済み:

2015 年 1 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by