For loop: Odd numbers turn 0 when checked if even
Mostrar comentarios más antiguos
Hello,
Sorry if the title is misleading. In short, when code checks if number is even; it assigns it to evens. However, if number is odd; it turns it into 0 and assigns it to even.
Here is the output...
48 55 6 66 89 11 44 29 99 61 26 14 55 83 84 84 21 55 88 13
48 0 6 66 0 0 44 0 0 0 26 14 0 0 84 84 0 0 88
48 is even, 55 is not so it turns into 0, 6 is even, and so on....
integers = randi([1 100], 1, 20);
evens = ones(1);
odds = ones(1);
for k = 1:length(integers)
if mod(integers(k), 2) == 0 % check if number is even
evens(k) = integers(k);
end
odds(k) = integers(k); % else if number is odd
end
Respuesta aceptada
Más respuestas (0)
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!