Updated February 27, 2023

## Introduction to Factorial in Matlab

MATLAB provides us with plenty of functionalities, useful in various computational problems. In this article, we will study a powerful MATLAB function called ‘MATLAB factorial’.

Before we understand how to calculate factorial in MATLAB, let us first refresh our understanding of factorial. Factorial of any positive integer ‘n’ is a product of all the whole numbers from 1 to n (both included). In Mathematics, an exclamation sign is used to represent the factorial of a number.

**Syntax of Factorial Function in Matlab**

`f= factorial(n)`

### Description of Factorial Function in Matlab

Here is the description mention below

#### 1. f = factorial(n)

- Here n is a non-negative integer value and this function will result in a product of all positive integers whose value will either be equal to ‘n’ or less than ‘n’
- Let us consider ‘n’ to be an array, then function ‘f’ will result in factorial of all values which are part of array ‘n’.The size of the output array and its data type will be same as of input array ‘n’
- Generally, it is represented as n! in maths but in MATLAB it will be written as f =factorial(n)

**Now let us understand the computation of factorial in MATLAB with various examples:**

Let us first take a simple example of calculating the factorial of a whole number.

##### Example #1

`f = factorial(5)`

This is how our input and output will look like in MATLAB console:

So, our output is the product of all whole numbers less than and equal to our input (excluding zero).

f = 5X4X3X2X1

f = 120

##### Example #2

`f = factorial(3)`

This is how our input and output will look like in MATLAB console:

**Next, we will understand how to take the factorial of an array.**

f = 3×2

f = 6

In the following example, let us take a 2 X 2 array.

##### Example #3

Input array:

[ 5 2 ;7 4]When our input array is passed to MATLAB, it will calculate factorial of each element in the array individually.

f = [factorial(1) factorial(5) ; factorial(3) factorial(2)]

f = 2×2

f = 1202

5040 24

This is how our input and output will look like in MATLAB console:

`n = [ 5 2 ;7 4];`

f = factorial(n)

As we can observe in our output, MATLAB has calculated factorial of each element present in the array, i.e 120, 2, 5040, 24 for 5, 2, 7, 4 respectively.

Next, let us take a 2 X 3 array

##### Example #4

n = [ 1 5 6 ;3 0 1]

Here also, when our input array is passed to MATLAB, it will calculate factorial of each element in the array individually.

f = [factorial(1) factorial(5) factorial(6) ; factorial(3) factorial(0) factorial(1)]

f = 2×3

f = 1 120 720

6 1 1

This is how our input and output will look like in MATLAB console:

`n = [ 1 5 6 ;3 0 1];`

f = factorial(n)

#### 2. For unsigned integer values

f = uintx(factorial(n))

It will convert the factorial n into an unsigned x 8-bit integer. Since the maximum value for an 8-bit integer is 255 so it will take the factorial of an integer whose value is beyond 255 to be 255 only.

Similarly, for x= 16, it will take the highest value to be 16-bit int value that is 65535.

**Let us understand with an example:**

##### Example #1

Here we will use unsigned 8-bit integer.

f= uint8 ([5 1 6])

f= 1X3 unit 8 row vector

f= 120 1 255

**This is how our input and output will look like in MATLAB console:**

`n = uint8([5 1 6]);`

f = factorial(n)

As we can see in our output, though factorial of 6 is 720 due to unsigned 8-bit integer it is converted to the highest 8-bit value which is 255

##### Example #2

In this example, we will take unsigned 16-bit integer

f = uint16 ([5 1 6 9])

f= 1X4 unit 16 row vector

f= 120 1 720 65535

This is how our input and output will look like in MATLAB console:

`n = uint16 ([5 1 6 9]);`

f = factorial(n)

As we can see in our output, though the factorial of 9 is 362880 due to unsigned 16-bit integer, it is converted to the highest 8-bit value which is 65535.

So, as we can see, MATLAB can be used to calculate factorial of any positive integer. Also, we can use unsigned integers as per our requirement. It can also be used to calculate the factorial of elements in an array.

### Recommended Articles

This is a guide to Factorial in Matlab. Here we discuss the description of Factorial in Matlab through various examples for better understanding. You may also look at the following articles to learn more –