for loop for different set of values
显示 更早的评论
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
采纳的回答
更多回答(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
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.
Rafiqul Islam
2022-10-31
Walter Roberson Thanks a lot
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!