MATLAB: Write a single line code to sum all the odd numbers of an array. arraynumbersoddsum The code should contain only one line. Best Answer You can use this code:y = sum(x(rem(x,2)==1)); Related SolutionsMATLAB: Sum of all odd and even arrays in matrix From your description, I think you want to do this:b = [sum(a(1:2:end,:)); sum(a(2:2:end,:))]However, I'm not sure why you wrote "b = [16;19]". If you want to get that, then just write:b = sum(a).' MATLAB: How to write a code that keeps all the even numbers in a vector and deletes all the odd numbers This is how you test if an integer is even.isEven = @(x) mod(x, 2) == 0isEven(1)isEven(2) Related QuestionSumming arrays within a cell array.Count even elements vectorOdd and even numbersFinding repetition numbers in array.
Best Answer