for loop for different set of values
86 ビュー (過去 30 日間)
古いコメントを表示
Hi, i want to run for loop with different set of values as for example
for i = 2:6,17:22
Y= 2*i;
End
Please do it ASAP
採用された回答
Tim
2015 年 6 月 22 日
編集済み: Tim
2015 年 6 月 22 日
A more polite way to ask in the event of urgency would be something like:
"I really need help on this one, time is a factor!"
a =[2:6 17:22]
for i=1:length(a)
y=2*a(i)
end
Or if you want to do it without a loop use vector form, which is generally more efficient in MATLAB:
a =[2:6 17:22];
y=2.*a;
0 件のコメント
その他の回答 (2 件)
Azzi Abdelmalek
2015 年 6 月 22 日
編集済み: Azzi Abdelmalek
2015 年 6 月 22 日
It's better to avoid the variable i, because it's used by Matlab with complex numbers.
for ii= [2:6,17:22]
Y= 2*ii;
end
0 件のコメント
Walter Roberson
2015 年 6 月 22 日
for i = [2:6,17:22]
Y = 2*i;
end
Note that you are overwriting Y completely each time, which is a common error in for loops. When you are looping over values that are not consecutive integers and you want to get one output value for each input value then use something like
ivals = [2:6,17:22];
for k = 1 : length(ivals);
i = ivals(k);
Y(k) = 2 * i;
end
2 件のコメント
Amir Mohammad Babaie Parsa
2022 年 5 月 25 日
編集済み: Amir Mohammad Babaie Parsa
2022 年 5 月 25 日
Hi
It really helped me, Thanks a zillion.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!