How to Select Random Rows from a Matrix in MATLAB?


In MATLAB, we can perform various operations on matrices. One such operation is selecting random rows from a matrix. MATLAB provides several different methods of selecting random rows from a matrix. In this tutorial, I will explain different methods of selecting rows from a matrix randomly using MATLAB.

Select Random Rows from a Matrix in MATALB

In MATLAB, we have various built-in functions that can be used to randomly select rows from a matrix.

The following are some commonly used functions to randomly select rows from a matrix −

  • randperm() Function

  • randsample() Function

  • randi() Function

  • datasample() Function

Let us discuss the utilization of these functions to randomly select rows from a matrix in MATLAB.

randperm() Function in MATLAB

The "randperm()" function is a built-in function in MATLAB library. This function generates a random permutation of input numbers.

We can use this function to generate the random permutation of row indices of a matrix. Then, this randomly generated permutation can be used to select random rows from the matrix.

The steps involved in selecting random rows from a matrix using the "randperm" function in MATLAB are given below.

  • Step 1 − Create a matrix.

  • Step 2 − Specify the number of rows to be selected randomly.

  • Step 3 − Determine the number of rows in the matrix.

  • Step 4 − Generate random permutation of the row indices of the matrix using the "randperm" function.

  • Step 5 − Select the rows corresponding to the random row indices generated in the step-4.

  • Step 6 − Display the results.

Example 1

Let us understand the steps with the help of an example in MATLAB.

% MATLAB code to select random rows from matrix using randperm function
% Create a sample matrix
mat = [10 20 40 60; 12 14 10 74; 40 50 80 90; 30 12 35 24; 86 24 74 36];

% Specify the number of rows to select
num_rows = 2;	% Adjust as required

% Determine the number of rows in the matrix
total_rows = size(mat, 1);

% Generate random permutation of row indices
p = randperm(total_rows, num_rows);

% Select rows according to random row indices
random_rows = mat(p, :);

% Display the input matrix and selected random rows
disp('Input Matrix:');
disp(mat);

disp('Randomly Selected Rows:');
disp(random_rows);

Output

Here is the output −

Input Matrix:
    10    20    40    60
    12    14    10    74
    40    50    80    90
    30    12    35    24
    86    24    74    36
Randomly Selected Rows:
    40    50    80    90
    30    12    35    24

This is how we can use the "randperm" function in MATLAB to select random rows from a matrix. When you will run this code, it will return different rows from the matrix selected randomly.

randsample() Function in MATLAB

In MATLAB, the "randsample()" function can be used to select random rows from a matrix. This function is used to get the indices of the randomly selected rows. Then, these indices are used to select the random rows from the matrix.

The step-by-step process for selecting random rows from a matrix using the "randsample" function is explained below.

  • Step 1 − Create a matrix.

  • Step 2 − Specify the number of random rows to select.

  • Step 3 − Use the "randsample" function to get the indices of randomly selected rows.

  • Step 4 − Use the random indices to select the random rows from the matrix.

  • Step 5 − Display the results.

Example 2

Let us see an example in MATLAB programming to implement these steps.

% MATLAB code to select random rows from matrix using randsample function
% Create a sample matrix
mat = [10 20 40 60; 12 14 10 74; 40 50 80 90; 30 12 35 24; 86 24 74 36];

% Specify the number of rows to select
num_rows = 2;	% Adjust as required

% Determine the number of rows in the matrix
total_rows = size(mat, 1);

% Get the indices of randomly selected rows
p = randsample(total_rows, num_rows);

% Select rows according to random row indices
random_rows = mat(p, :);

% Display the input matrix and selected random rows
disp('Input Matrix:');
disp(mat);

disp('Randomly Selected Rows:');
disp(random_rows);

Output

When you run this code, it will produce the following output −

Input Matrix:
    10    20    40    60
    12    14    10    74
    40    50    80    90
    30    12    35    24
    86    24    74    36
Randomly Selected Rows:
    40    50    80    90
    86    24    74    36

randi() Function in MATLAB

In MATLAB, we can also randomly select rows from a matrix using the "randi" function.

The steps involved in selecting random rows from a matrix using the "randi" function are same as that of the above two methods.

Example 3

Let us see an example to understand the use of the "randi" function to select random rows from a matrix.

% MATLAB code to select random rows from matrix using randi function
% Create a sample matrix
mat = [10 20 40 60; 12 14 10 74; 40 50 80 90; 30 12 35 24; 86 24 74 36];

% Specify the number of rows to select
num_rows = 2;	% Adjust as required

% Determine the number of rows in the matrix
total_rows = size(mat, 1);

% Generate indices of random rows
p = randi(total_rows, 1, num_rows);

% Select rows according to random row indices
random_rows = mat(p, :);

% Display the input matrix and selected random rows
disp('Input Matrix:');
disp(mat);

disp('Randomly Selected Rows:');
disp(random_rows);

Output

Here is the output −

Input Matrix:
    10    20    40    60
    12    14    10    74
    40    50    80    90
    30    12    35    24
    86    24    74    36
Randomly Selected Rows:
    10    20    40    60
    12    14    10    74

datasample() Function in MATLAB

In MATLAB, there is another function named "datasample" that can be used to randomly select rows from a matrix.

The following steps are to be followed to select the random rows from a matrix using the "datasample" function.

  • Step 1 − Create a matrix.

  • Step 2 − Specify the number of rows to be selected randomly from the matrix.

  • Step 3 − Use the "datasample" function to randomly select the rows from the matrix.

  • Step 4 − Display the randomly selected rows.

Example 4

Let us take an example to understand the implementation of these steps in MATLAB.

% MATLAB code to select random rows from matrix using datasample function
% Create a sample matrix
mat = [10 20 40 60; 12 14 10 74; 40 50 80 90; 30 12 35 24; 86 24 74 36];

% Specify the number of rows to select
num_rows = 3;	% Adjust as required

% Select the rows randomly from the matrix
random_rows = datasample(mat, num_rows);

% Display the input matrix and selected random rows
disp('Input Matrix:');
disp(mat);

disp('Randomly Selected Rows:');
disp(random_rows);

Output

Here is the output −

Input Matrix:
    10    20    40    60
    12    14    10    74
    40    50    80    90
    30    12    35    24
    86    24    74    36
Randomly Selected Rows:
    40    50    80    90
    86    24    74    36
    10    20    40    60

Conclusion

In conclusion, MATLAB provides various built-in functions that we can use to randomly select rows from a matrix. In this article, I explained the four most commonly used functions to select random rows from a matrix in MATLAB. You can try these codes with different matrices and different number of random rows.

Updated on: 25-Oct-2023

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements