MATLAB: How to perform a different calculation on the elements in an array based on whether they are odd or even

evenodd

This may be really simple/ already asked so I apologise in advance!!!
I made an array: x = 1:3:90 I am then supposed to multiply every even element in the array by 2, and square every odd element. I am currently trying to do a for loop for it, and have used rem(x,2) to find out which positions are odd/even, but i am lost on how to proceed.
thank you!

Best Answer

There are several ways to approach this. I would use ‘logical indexing’, and go from there:
x = 1:3:90;
even_x = rem(x,2) == 0; % Logical Index Of Even Values
odd_x = rem(x,2) ~= 0; % Logical Index Of Odd Values
I leave the rest to you.