Get the factorial of an array

11 ビュー (過去 30 日間)
Mario Youchia
Mario Youchia 2020 年 5 月 8 日
回答済み: James Tursa 2020 年 5 月 8 日
How can I create a function that calculates the factorial of each number in an array and returns another array which is the factorial.
The numbers are any positive integers and they haven't a specific order, for example [1 9 1 3 7 2 5 4 0 9]; I want to input this so the output would be [1 362880 1 6 5040 2 120 24 1 362880].
Thanks in advance.

回答 (2 件)

James Tursa
James Tursa 2020 年 5 月 8 日
The factorial( ) function is vectorized, so just call it:
>> n = [1 9 1 3 7 2 5 4 0 9];
>> factorial(n)
ans =
Columns 1 through 8
1 362880 1 6 5040 2 120 24
Columns 9 through 10
1 362880

Ameer Hamza
Ameer Hamza 2020 年 5 月 8 日
編集済み: Ameer Hamza 2020 年 5 月 8 日
x = [1 9 1 3 7 2 5 4 0 9];
y = arrayfun(@factorial, x);
Result
>> y
y =
Columns 1 through 6
1 362880 1 6 5040 2
Columns 7 through 10
120 24 1 362880

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by