# Sum of element in A vector / Matrix

clear; clc
disp(' ')
disp(' *** This script calculates the sum of any vector ***')
disp(' *** The elements must be numeric ***')
disp(' ')
v = input('Please enter a vector of any length: ');
[r c] = size(v);
if (r > 1 & c > 1)
fprintf('\nYou have entered a %d by %d matrix indicated below:\n\n',r,c)
disp(v)
fprintf('The matrix has been converted to a %d by 1 column vector as appears in column 2:\n',r*c)
%v = v';
n = r*c;
sum = 0;
fprintf('\n ------------------------\n')
fprintf(' i vector cumsum\n')
fprintf(' ------------------------\n')
for i = 1:n
sum = sum + v(i);
Table = [i v(i) sum];
fprintf(' %2.0f %3.0f %3.0f\n',Table)
end
fprintf(' ------------------------\n\n')
end
if (r == 1 | c == 1)
fprintf('\nYou have entered a vector with %d elements as appears in column 2:\n', length(v))
% disp(v);
n = length(v);
sum = 0;
fprintf('\n ------------------------\n')
fprintf(' i vector cumsum\n')
fprintf(' ------------------------\n')
for i = 1:n
sum = sum + v(i);
fprintf(' %2.0f %3.0f %3.0f\n',i,v(i),sum)
end
fprintf(' ------------------------\n\n')
end

Modelling in Mathematical Sciences