Write an mfile using conditional statements and loops that w
Write an m-file using conditional statements and loops that will determine and display the summation of the positive terms of a vector, and the sum of the negative elements of a vector. To test your program use the vector v = [15 -6 0 8 -2 5 4 -10 0.5 3].
Solution
function [spn] = summ(a,b)
clc;
v=0;
sz=0; % variable for length of vector
sp=0; % variable for sum of positive terms
sn=0; % variable for sum of negetive terms
v = [15 -6 0 8 -2 5 4 -10 0.5 3];
sz = length(v)
for i= 1:1:sz
if v(i)<0
sn=sn+v(i);
else
sp=sp+v(i);
end
end
display(\'the sum of negetive terms in the vector is\'),
sn
display(\'the sum of positive terms in the vector is\')
sp
end
