In today’s world of high-performance computing tools, finding factorial manually is an impractical approach. MATLAB provides us with a built-in function to compute the factorial of a number in a quick and efficient way.
This tutorial will present the method to find the factorial of a number or array in MATLAB.
What is Factorial in MATLAB?
Factorial of a non-negative integer n can be defined as a product of all positive integers less than or equal to the number n. In mathematics, it is denoted by a (!) symbol and has the following mathematical form:
How to Find the Factorial in MATLAB?
In MATLAB, we can compute the factorial of a non-negative integer using the built-in factorial() function. This function takes a scalar value or an array as an input and returns the computed factorial value as an output.
The factorial() function can be implemented in MATLAB through the following syntax:
The function f = factorial(n) is responsible for computing the factorial of the given number n.
- If n represents a scalar, the value of its calculated factorial will be a scalar number having the same size and data type as the input scalar value n.
- If n represents an array, this function will calculate the factorial of each value having the same size and data type as the input array.
Example 1: How to Find the Factorial of a Scalar Value in MATLAB
This MATLAB code determines the factorial of the given scalar number n=100 using the factorial() function.
f = factorial(n)
Example 2: How to Compute the Factorial of an Array in MATLAB
In this example, we create a 10-by-10 square matrix using the magic() function and use the factorial() function to compute the factorial of the given matrix A.
A_f = factorial(A)
Finding the factorial of an integer is a mathematical task that equals the product of an integer with all positive values less than or equal to that integer. In MATLAB, this task can be effectively performed using the built-in factorial() function. This guide has provided the implementation of the factorial() function with examples for a better understanding of how to use it in MATLAB.