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 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](/WebImages/25/write-a-program-using-a-loop-to-add-all-the-elements-of-a-ve-1063290-1761555787-0.webp)