if-else if inside the for loop

1 visualización (últimos 30 días)
Turbulence Analysis
Turbulence Analysis el 18 de Feb. de 2023
Comentada: Sulaymon Eshkabilov el 18 de Feb. de 2023
The numbers 1 to 24 is stored inside the array i
i =[ 1 ..24]
Here, lets say for i = 1,2,4,5,6 and 13,14,16,17,18 I have to define a = 3 and rest of the numbers I have to define a = 4. How to implement this using if else inside the for loop.
for
if (i = 1,2,4,5,6,13,14,16,17,18)
a=3;
else
a=4
end
end
  2 comentarios
Turbulence Analysis
Turbulence Analysis el 18 de Feb. de 2023
Thanks very much !
Sulaymon Eshkabilov
Sulaymon Eshkabilov el 18 de Feb. de 2023
Most welcome! Glad to help.

Iniciar sesión para comentar.

Respuesta aceptada

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 18 de Feb. de 2023
Here is how to do it in for .. end loop
N = 1:24;
for ii=1:length(N);
if ismember(ii, [1,2,4,5,6,13,14,16,17,18])
a(ii)=3;
else
a(ii)=4;
end
end
a
a = 1×24
3 3 4 3 3 3 4 4 4 4 4 4 3 3 4 3 3 3 4 4 4 4 4 4

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by