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

Would you pleas help me with the following? Using M-files define an input vector

ID: 3847310 • Letter: W

Question

Would you pleas help me with the following? Using M-files define an input vector x = [1 3 4 15 8 5 3 10 15 21 6 9 18] Write a MATLAB program that will add all the numbers corresponding to the even indices of an array. For instance, if the array x was x = [1, 3, 5, 10], then it should return 13 (=3 +10). Use that program to find the sum of all even integers from 1 to 17. Write your program so that it is flexible. That is, you should be able to invoke your program from the command window as follows: > > y = addeven(x) % is the input vector, and y is the sum of all the numbers corresponding to the even indices of x.

Explanation / Answer

Create a file addeven.m And paste below code in it.

function [summ] = addeven(arr)
summ = 0;
i = 2;
while(i <= size(arr,2))
summ = summ + arr(i);
i = i + 2;
end
end

Sample input:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote