Write a script that prompts the user to enter a vector named

Write a script that prompts the user to enter a vector, named vector, which has even and odd integers. For example, the vector [26 15 -3 4 78 6 5 1 9 18]. Create a new vector, named evens, containing only the even integers in vector. The scripts should work with vectors of any length.

Solution

Use the following code for the MATLAB.

% Create sample data.
x = [26 15 -3 4 78 6 5 1 9 18]
% Find indices where x is even:
evenIndices = rem(x, 2) == 0
% Extract only the even numbers into a new vector.
allTheEvenNumbers = x(evenIndices)
x = [26 15 -3 4 78 6 5 1 9 18]
% Find indices where x is odd:
oddIndices = rem(x,2)~=0
% Extract only the odd numbers into a new vector.
allTheOddNumbers = x(oddIndices)

 Write a script that prompts the user to enter a vector, named vector, which has even and odd integers. For example, the vector [26 15 -3 4 78 6 5 1 9 18]. Crea

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site