How to create a loop to sum up the elements in a row array one by one?
Show older comments
Hi all, I have created a row array using below code,
for k = 1:500
c(k)=1/N.*(ecg(k).*exp(-1*i*k*(2*pi/N*t(k))));
end
and I would like to sum up the elements in the row array one by one so that I can have something like this:
a=[ 1 1 1 1 1] %example only
for k=1:5
=> a=[1 2 3 4 5 ] %final answer after the for loop
However, I have no idea what to do next. Can someone help me?
Answers (1)
a=[1 1 1 1 1];
cumsum(a)
If you have a matrix, and you want to take your sums row-wise, just use the second argument (dim) to specify you want rows:
a=[1 2 3;4 5 6]
cumsum(a)
cumsum(a,2)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!