for loop indexing and skip numbers

12 ビュー (過去 30 日間)
Damith
Damith 2014 年 5 月 28 日
コメント済み: Abraxas 2019 年 4 月 25 日
Hi,
I need to include for loop in MATLAB to start with 2:13. But I need to start with 2,3,4 and skip 5 and again 6,7,8 and skip 9 and 10,11,12 until 13.
How can I implement this is for loop?
Many thanks in advance.

採用された回答

Roger Stafford
Roger Stafford 2014 年 5 月 28 日
You can also use
ix = floor((4*(1:n)+2)/3);
where n is the desired length of the output.
  1 件のコメント
Damith
Damith 2014 年 5 月 28 日
Thanks. I appreciate it.

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

その他の回答 (3 件)

the cyclist
the cyclist 2014 年 5 月 28 日
for n = [2 3 4 6 7 8 13]
disp(n)
end
  5 件のコメント
Damith
Damith 2014 年 5 月 28 日
Thanks. It worked.
Abraxas
Abraxas 2019 年 4 月 25 日
I've been searching 2 days for this answer! Thank you so much!

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


the cyclist
the cyclist 2014 年 5 月 28 日
Yet another method:
idx = bsxfun(@plus,[1 2 3]',1:4:100)
for ii = idx(:)
disp(ii)
end
  1 件のコメント
Damith
Damith 2014 年 5 月 28 日
Thanks. it's working too.

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


the cyclist
the cyclist 2014 年 5 月 29 日
Yet another method:
idx = filter([1 1 1 1],[1 1 1 0],1:100)+1
  1 件のコメント
Damith
Damith 2014 年 5 月 29 日
Thanks.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by