Write a program using a loop to add all the elements of a ve

Write a program using a loop to add all the elements of a vector. Do not use the MATLAB function sum function or +. For example, [-1 5 10-2 32 -5] SUM = 39

Solution

function for sum

function result = sumPositive(vector)
%-------------------------------------------------
result = 0;
for k = 1:1:length(vector)
if (vector(k) >= 0)
result = result + vector(k);
end
end

vector = [-1,5,10,-2,32,-5];
result = sumPositive(vector);
disp(\'SUM=\')
disp(result)

 Write a program using a loop to add all the elements of a vector. Do not use the MATLAB function sum function or +. For example, [-1 5 10-2 32 -5] SUM = 39Solu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site