Matlab

How to Use randperm() Function in MATLAB?

Generating random numbers is a straightforward task because MATLAB provides users with a wide range of functions that can be used for generating a scalar, a vector, or a matrix of random numbers. These functions are used to generate random numbers in different distributions according to their functionalities. The randperm() function is among those functions that can be used to generate a row vector containing random permutations of integers.

In this article, we are going to explore the functionality of the randperm() function in MATLAB using examples.

How to Use randperm() Function in MATLAB?

The randperm() is a built-in function in MATLAB that allows us to generate a row vector having a random permutation of the integers from 1 to the specified number n. The function is only used for generating a row vector containing the random permutation of integers.

This function follows several syntaxes that are given below:

X = rand(n)

X = rand(n,k)

Here:

The function X = randperm(n) generates the row vector having the random permutation of the integers range from 1 to n without repeating numbers.

The function X = randperm(n,k) yields a row vector with k distinct integers chosen at random from the range of 1 to n.

Consider some examples that demonstrate the use of the randperm() function in MATLAB.

Example 1

In this given MATLAB code, we generate a vector containing integer values randomly permuted from 1 to 4 using randperm(n) function for n = 4.

X = randperm(4)

Example 2

In this given example, we generate a vector containing 5 integer values randomly permuted from 1 to 6 using the randperm(n,k) function for n = 6 and k = 5.

X = randperm(6,5)

Conclusion

The randperm() is a built-in function in MATLAB that is used for generating a row vector containing randomly permuted integer values from 1 to a specified number n. This function only generates a row vector containing randomly permuted integer values. This tutorial taught us how to generate randomly permuted integer values using the randperm() function 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.