MATLAB: How to sum vectors in a loop where it cumulatively sums with the vector created in the previous loop

cummulativefor loopMATLABvector summation

I am currently trying to create a loop where the code sums up numbers from the previous total each time. I have the variable dispSum which adds the displacement vector from row 1 + row 2. Then I want to add dispSum (row1+row2) with the displacment vector from row 3. This would continue until all the displacement vectors have been used so the code is constantly adding the total value. Currently what I have is dispSum adds the previous vector with the next row vector so I am not getting a grand total. How do I change this?
rad1 = [-30.4 -30];
K = 0;
disp = [];
for j = 1:length(medXY1);
K = K+1;
disp(K,1) = medXY2(j,1)-medXY1(j,1);
disp(K,2) = medXY2(j,2)-medXY1(j,2);
end
L0 = dot(disp(1,:),rad1);
k = 1;
dispSum = [];
L = [];
strain = [];
%dispSum1 = plus(disp(1,:),disp(2,:));
for j = 1:length(disp)-1;
dispSum(j,:) = plus(disp(j,:),disp(j+1,:));
L(j,:) = dot(dispSum(j,:),rad1);
strain = (L-L0)/L0;
end

Best Answer

I think you want to use cumsum. Also, you should take advantage of MATLAB's ability to handle matrices.
rad1 = [-30.4 -30];
disp = medXY2-medXY1;
L0 = dot(disp(1,:),rad1);
dispSum = cumsum(disp);
L = dot(dispSum,ones(length(disp),1)*rad1,2);
strain = (L-L0)/L0;