필터 지우기
필터 지우기

How to write cumulative product function

조회 수: 1 (최근 30일)
Life is Wonderful
Life is Wonderful 2022년 3월 15일
댓글: Life is Wonderful 2022년 3월 16일
Hi
I am trying to implement cumproduct without using built-in function. Below is my test code . I need help to implement "My Test code " like built-in cumprod([1 1 2 : 5]).
Thank you!!
% Built-in
A = cumprod([1 1 2 : 5])
% A
%------+
% 1
% 1
% 2
% 6
% 24
% 120
% My Test code ,
A1 = 1;
for i = 1 :5
A1 = A1 * i;
fprintf('%10d|%10d|\n',i,A1);
end
A1
-----+
1|
2|
6|
24|
120|
  댓글 수: 2
Life is Wonderful
Life is Wonderful 2022년 3월 15일
Thank you!!
Voss
Voss 2022년 3월 15일
You're welcome!

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2022년 3월 16일
편집: Torsten 2022년 3월 16일
m = 10;
v = [1 1 2:m];
pcum = cumproduct(v)
function pcum = cumproduct(v)
n = numel(v);
pcum = zeros(n,1);
pcum(1) = v(1);
for i = 2:n
pcum(i) = pcum(i-1)*v(i);
end
end
  댓글 수: 3
Torsten
Torsten 2022년 3월 16일
Recursive call ?
Life is Wonderful
Life is Wonderful 2022년 3월 16일
Yes, would nice if you can share snippet !!
Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by