How to sum individual elements MATLAB -


suppose have row matrix [a1 a2 a3 a4 .. an] , wish achieve each of following in matlab

    1) 1+a1     2) 1+a1+a2     3) 1+a1+a2+a3     4) 1+a1+a2+a3+a4 ....      1+a1+a2+...+an 

how shall them?

this purpose of cumsum function. if a vector containing elements [a1 a2 a3 .. an] then

b = cumsum([1 a]); 

contains terms searching for. possibility is

b = 1 + cumsum(a); 

edit

if don't want use built-in function cumsum, simpler way go for loop:

% consider preallocation speed b = nan(numel(a),1);     % assign first element b(1) = 1 + a(1);  % loop = 2:numel(a)   b(i) = b(i-1) + a(i); end 

or, without preallocation:

b = 1 + a(1); = 2:numel(a)   b(end+1) = b(end) + a(i); end 

best,


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -