MATLAB: Sum of consecutive elememts

for loop homework

suppose i have a matrix A=[1 2 3 4 5] and i need another matix as sum of consecutive elememts i.e [3 5 7 9] how to do using loop

Best Answer

Without loop:
res=A(1:end-1)+A(2:end)
With loop:
for i=1:numel(A)-1
res(i)=A(i)+A(i+1);
end