MATLAB: Only want last iteration displayed, whereas it continually sums up the array each loop

for loop

x = [1 23 43 72 87 56 98 33]
summ = 0
for k = 1:length(x)
summ = summ + x(k)
end

Best Answer

x = [1 23 43 72 87 56 98 33] ;
summ = 0 ;
for k = 1:length(x)
summ = summ + x(k) ;
end
fprintf('sum=%f\n',summ)
You can look into inbuilt function sum to get sum.
thesum = sum(x)