MATLAB: Odd and even numbers

MATLABodd

Hi I'm new in matlab so i need a little help to get started. How do i make a program which can distinguish from odd and even numbers? I know that i need to make a loop, it should be either if or while but i would love some suggestions on how to solve this problem.
Thanks 🙂

Best Answer

You simply have to go back to the definition of odd and even. An (integer) number is even if it is divisible by 2, odd otherwise. Divisible by 2 means that the remainder when divided by 2 is 0. That is easy to test, the function to get the remainder is rem (or you can use mod).
As with many things in matlab you do not need a loop, the functions work on vector / matrices
m = [1 2 3;4 5 6]
iseven = rem(m, 2) == 0