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 :)
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 (or you can use ). 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;
m = [1 2 3; 4 5 6] iseven = rem(m,2) == 0; m_even = m(iseven) isodd = rem(m,2) == 1; m_odd = m(isodd)
If you want to execute some statements based on whether the positive number in m is even or odd, you can make a conditional statement within a “for” loop:
for i = 1:numel(m) if rem(m(i),2) == 0 % statements to execute for even m(i) else % statements to execute for odd m(i) end end
Otherwise, if you have a matrix of integer numbers that can be negative in general, use "mod" instead of "rem" in the above example.
Matlabsolutions.com provides guaranteed satisfaction with a
commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain
experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support
to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been
empanelled after extensive research and quality check.
Matlabsolutions.com provides undivided attention to each Matlab
assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services
include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work
done at the best price in industry.