How to create a loop to sum up the elements in a row array one by one?
조회 수: 1 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (1개)
Dave B
2021년 10월 31일
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)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!