increase factorial execution speed

조회 수: 4 (최근 30일)
Life is Wonderful
Life is Wonderful 2022년 9월 2일
댓글: Life is Wonderful 2022년 9월 5일
Hello there
I'm attempting to use the prod function to construct the factorial code more quickly, but I'm encountering a problem.
Please let me know the reason for this and what I might be doing incorrectly.
Many thanks
n = fi(1); % n max is 20
n =
1 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 14
indx = max(1,max(min(n,20)));
for g = 1:indx
out(:) = prod(1:indx,'all');
end
Error using prod
Invalid data type. First argument must be numeric or logical.

채택된 답변

Chunru
Chunru 2022년 9월 2일
편집: Chunru 2022년 9월 2일
n = 5 % n max is 20
n = 5
out = cumprod(1:n)
out = 1×5
1 2 6 24 120
n = fi(5); % n max is 20
out = (1:n);
for i = 2:n
out(i) = out(i-1)*i;
end
out
out =
1 2 6 24 120 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 0
  댓글 수: 6
Chunru
Chunru 2022년 9월 2일
See above for the fi data type.
Life is Wonderful
Life is Wonderful 2022년 9월 5일
Thanks a lot,

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by