필터 지우기
필터 지우기

how to use loop for sum without using cumsum?

조회 수: 1 (최근 30일)
Arvind
Arvind 2023년 6월 26일
편집: Aakash 2023년 6월 26일
i want sum without using 'cumsum'
can i get s with loop:
s1 = x1;
s2 = x1+x2;
s3 = x1+x2+x3
s4= x1+x2+x3+x4; and so on upto s100
s = [s1;s2;s3;s4......;s100]

채택된 답변

Aakash
Aakash 2023년 6월 26일
편집: Aakash 2023년 6월 26일
You can do this:
%sample data
x = [1, 2, 3, 4];
n = length(x);
s = zeros(n, 1); % Initialize s as a vector of zeros
for i = 1:n
s(i) = sum(x(1:i)) % Calculate the sum of the first i elements of x
end
or
s = zeros(n, 1); % Initialize s as a vector of zeros
s(1)=x(1);
for i = 2:n
s(i) = x(i)+s(i-1) % Calculate the sum of the first i elements of x
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by