Matlab

How to Find Factorial in MATLAB

Finding the factorial of a number is a widely used mathematical operation that is used to solve different mathematical problems such as combination, Taylor series, and many more. This is a time-taking task that needs many calculations while finding the factorial of a large number.

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:

N! = N* (N-1) * (N-2) * (N-3) * …. * N-(N-1)

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.

Syntax
The factorial() function can be implemented in MATLAB through the following syntax:

f = factorial(n)

Here,
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.

n = 100;
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 = magic(10);
A_f = factorial(A)

Conclusion

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.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.