Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use Matlab to solve this problem 7) Write a function extract_odds that ta

ID: 2267758 • Letter: P

Question

Please use Matlab to solve this problem

7) Write a function extract_odds that takes an array of random integers (you can generate this with the Matlab command v randi(10 50],[100, 11)) and returns only the elements of the array that are even, using an if...else statement. 8) Write a function extract_odds_or_evens that takes two inputs: an array of random integers like in (7) and a second parameter that is either the string 'odd' or 'even'. Use combinations of a for loop and up to two if...else statements to extract either the odd or even numbers based on the second parameter and return the result.

Explanation / Answer

question 7:

function ev = extract_odds(v)
ev = [];%creating an emptry vector
for i=1:length(v)
if mod(v(i),2)==0
ev = [ev v(i)];%appending even numbers to ev
end
end
end

question 8:

Matlab Script:

function odd_or_even = extract_odds_or_evens(v,str)

odd_or_even = [];%creating an emptry vector
for i=1:length(v)
if strcmp(str,'even')
mul = 0;
else
mul = 1;
end
if mod(v(i)+mul,2)==0  %observe mul added makes a odd number to even when str is odd
odd_or_even = [odd_or_even v(i)];%appending even numbers to ev
end
end

end