- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Randomly Shuffle Rows in MATLAB Matrix
MATLAB is a great tool to process matrices. It provides a large number built-in functions and processing methods that we can use to manipulate our matrices just by writing a small piece of MATLAB codes. Such an operation that we can perform on matrices in MATLAB is random shuffling of rows of a matrix. In this tutorial, I will explain how you can randomly shuffle rows of a matrix using MATLAB.
Randomly Shuffle Rows of a Matrix in MATLAB
MATLAB is a complete solution to manipulate matrices using computers. In MATLAB, we can very easily shuffle rows of a matrix by using MATLAB's built-in functions.
MATLAB has a built-in function namely, 'randperm' that we can use to shuffle or swap the rows of a given matrix randomly. This function shuffles the rows of the matrix by generating a random permutation of indices of rows. This randomly generated permutation allows us to rearrange the rows of our matrix in a random order.
Before going to discuss the practical implementation of the concept, let us first discuss the step-by-step process of shuffling of rows of a matrix in MATLAB.
Process of Randomly Shuffling Rows of a Matrix using MATLAB
The step-by-step procedure to shuffle the rows of a matrix randomly using MATLAB programming is explained below:
Step (1) – First of all, create or load the matrix that you want to manipulate in the MATLAB workspace.
Step (2) – Determine the number of rows in the matrix. For this you can use a MATLAB's built-in function 'size'. The following syntax of the 'size' function is used to determine the number of rows in a matrix:
Rows_Num = size(matrix, 1);
Here, '1' is used to specify that we want to determine first dimension (row) of the matrix.
Step (3) – Now, use the 'randperm' function to generate a random permutation of indices of matrix rows. For this, the following syntax is to be used:
Rand_Perm = randperm(Row_Num);
Step (4) – In this step, utilize the randomly generated permutation to reorder/shuffle the rows of the matrix.
Step (5) – Finally, display the result.
We now have the enough knowledge to implement the MATLAB code to perform random shuffling of rows of a matrix.
Let us take an example program to understand how actually we can write the codes in MATLAB to shuffle the rows of a given matrix.
% MATLAB code to shuffle rows of a matrix randomly % Create a sample matrix Mat = [10, 20, 2; 42, 51, 67; 57, 85, 19; 1, 15, 2]; % Determine the number of rows in the matrix n = size(Mat, 1); % dim = 1 to specify rows % Generate a random permutation of indices of rows of the matrix R = randperm(n); % Shuffle the rows of the matrix using random permutation Shuffled_Mat = Mat(R, :); % Display the input and shuffled matrices disp('The input matrix is:'); disp(Mat); disp('The shuffled matrix is:'); disp(Shuffled_Mat);
The input matrix is: 10 20 2 42 51 67 57 85 19 1 15 2 The shuffled matrix is: 10 20 2 1 15 2 42 51 67 57 85 19
In this MATLAB code, we shuffled the rows of the input matrix using the randomly generated permutation.
This is all about random shuffling of rows of a matrix using MATAB. In this tutorial, we explained the step-by-step process to shuffle the rows of a matrix with the help of an example.
In conclusion, MATLAB has a built-in function 'randperm' that can be used to randomly shuffle the rows of a matrix by utilizing the randomly generated permutation of row indices of the matrix.
Kickstart Your Career
Get certified by completing the courseGet Started