For loop or while loop? help please

Dear sir,
I have a simple code
for k=1:3
for i=1:7
do something
end
end
If k=1 I would like the loop to execute the body from i= 1 to 7 if k=2 or k=3 I would like the loop to execute the body from i = 1 to 6.
How can I do that please? I don't want to repeat the body in the if condition. Thank you very much
Kind Regards
S

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 29 日
編集済み: Azzi Abdelmalek 2014 年 7 月 29 日

0 投票

m=7
for k=1:3
if k>1
m=6
end
for i=1:m
do something
end
end
%or
for k=1:3
for i=1:7-(k~=1)
%do
end
end

1 件のコメント

Saad
Saad 2014 年 7 月 29 日
thank you Azzi

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

その他の回答 (1 件)

Joseph Cheng
Joseph Cheng 2014 年 7 月 29 日

0 投票

or
for k=1:3
if k==1,
iMax = 7
else
iMax = 6
end
for i =1:iMax
do something
end
end
in this case you can change it such that if you do need to vary the iMax you can change the condition.

1 件のコメント

Saad
Saad 2014 年 7 月 29 日
thank you Joseph

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

カテゴリ

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

製品

タグ

質問済み:

2014 年 7 月 29 日

コメント済み:

2014 年 7 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by