for loop for different set of values
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (2)
Azzi Abdelmalek
el 22 de Jun. de 2015
Editada: Azzi Abdelmalek
el 22 de Jun. de 2015
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
el 22 de Jun. de 2015
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 comentarios
Amir Mohammad Babaie Parsa
el 25 de Mayo de 2022
Editada: Amir Mohammad Babaie Parsa
el 25 de Mayo de 2022
Hi
It really helped me, Thanks a zillion.
Rafiqul Islam
el 31 de Oct. de 2022
Walter Roberson Thanks a lot
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!