How to Randomly Shuffle Columns in Matrix in MATLAB?


MATLAB is an efficient tool to process and manipulate matrices. We can use it to shuffle the columns in a matrix. For this, MATLAB provides built-in functions that can be used together to shuffle the columns in a matrix. In this tutorial, I will explain how you can randomly shuffle columns in a matrix using MATLAB.

How to Random Shuffle Columns of Matrix Using MATLAB?

MATLAB provides some built-in functions that we can use to shuffle the columns in a matrix.

We have to follow the steps given below to shuffle the columns in a matrix using MATLAB.

  • Step (1) − Create a matrix, having multiple columns.

  • Step (2) − Determine the number of columns in the matrix using the "size" function.

col_num = size(matrix, 2);
  • Step (3) − Randomly arrange the indices of the columns in the matrix. For this, use the "randperm" function.

random_cols = randperm(col_num);
  • Step (4) − Replace the columns of the original matrix with the randomly arranged columns.

  • Step (5) − Display the output matrix with shuffled columns.

We can follow these five steps to randomly shuffle the columns in a matrix using MATLAB.

Example

Let us now understand the process of shuffling the columns in a matrix randomly.

% MATLAB code to randomly shuffle columns in matrix
% Create an example matrix
mat = [5 2 4 3; 4 5 2 6; 7 6 8 4];

% Determine the number of columns in the matrix
col_num = size(mat, 2);

% Randomly arrange the column indices of the matrix
rand_cols = randperm(col_num);

% Shuffle the columns using the random column indices
shuffled_mat = mat(:, rand_cols);

% Display the original and shuffled columns matrices
disp('Original Matrix:');
disp(mat);
disp('Shuffled Columns Matrix:');
disp(shuffled_mat);

Output

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

Original Matrix:
     5     2     4     3
     4     5     2     6
     7     6     8     4
Shuffled Columns Matrix:
     3     2     4     5
     6     5     2     4
     4     6     8     7

Explanation

In the output of this example, we can see that the columns of the matrix are randomly shuffled.

  • In this MATLAB example, we start by creating an example matrix. Then, we determine the number of columns in the matrix by using the "size" function, where "2" representing the second dimension "column" of the matrix.

  • After that, we randomly permute the column indices of the matrix using the "randperm" function.

  • Then, we use these randomly generated column indices to rearrange/shuffle the columns of the original matrix.

  • Finally, we use the "disp" function to display the original matrix and the matrix with shuffled columns.

Conclusion

In conclusion, shuffling the columns in a matrix is nothing but rearranging the columns in the matrix differently. MATLAB provides two built-in functions named "size" and "randperm" that we can use together to randomly shuffle the columns in a matrix.

In this tutorial, I explained the steps involved in random shuffling of columns in a matrix with the help of an example in MATLAB. You can also use this code with different matrices by replacing the matrix stored in the "mat" variable.

Updated on: 06-Oct-2023

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements