How Can Be Randomly Shuffle Rows in MATLAB Matrix?
Last Updated :
02 Sep, 2021
In this article, we are going to discuss the "random shuffling of rows in a Matrix " which can be done using size() and randperm() function.
Functions Used
1) size(): The size() function is used to return the sizes of each dimension of the specified array "X" or the size of the specified matrix "X".
Syntax:
size(X)
[m,n] = size(X)
size(X,dim)
[d1,d2,d3,...,dn] = size(X)
Here,
size(X) returns the sizes of each dimension of the specified array "X" in a vector d with ndims(X) elements.
[m,n] = size(X) returns the size of the specified matrix "X" in the separate variables m and n.
size(X,dim) returns the size of the dimension of "X" specified by scalar dim.
[d1,d2,d3,...,dn] = size(X) returns the sizes of the first n dimensions of the specified array "X" in separate variables.
Parameters: This function accepts two parameters which are illustrated below:
- X: It is the specified array or matrix or dimension.
- dim: It is the scalar value for the specified dimension "X"
2) randperm(): The randperm() function is used for the random permutation of integers.
Syntax:
randperm(n)
randperm(n,k)
Here,
randperm(n) returns a row vector that contains a random permutation of the integers from "1" to "n" without of any repetition.
randperm(n,k) returns a row vector that contains "k" number of unique integers that are selected randomly from 1 to n.
Parameters: This function accepts two parameters which are illustrated below:
- n: This is the specified number up to which a random number is going to be generated from "1" without any repetition.
- k: It is the number of unique integers that are selected randomly from 1 to n.
Below examples are for the "random shuffling of columns in a Matrix " which can be done using the combination of the size() and randperm() functions:
Example 1
Matlab
% MATLAB code for random shuffling
% of columns in a Matrix
A = [1 2 3 % Specifying a 3*3 matrix
4 5 6
7 8 9];
% Calling the size() function over
% the above matrix which gives a row vector
% whose elements are the lengths of the
% corresponding dimensions of A
rows = size(A);
% Calling the randperm() function for the
% random permutation of the above matrix
% over its dimension of 3*3
P = randperm(rows);
% Getting the row wise randomly shuffled matrix
B = A(P, : )
Output
B =
1 2 3
7 8 9
4 5 6
Example 2
Matlab
% MATLAB code for random shuffling
% of columns in a Matrix
A = [1 2 3 4 % Specifying a 4*4 matrix
5 6 7 8
9 10 11 12
13 14 15 16];
% Calling the randperm() function to
% randomly shuffle the rows of matrix A
A(randperm(size(A, 2)), : )
Output
ans =
9 10 11 12
1 2 3 4
5 6 7 8
13 14 15 16
Similar Reads
How to Randomly Shuffle Columns in MATLAB in Matrix? In this article, we are going to discuss the "random shuffling of columns in a Matrix " with the help of size() and randperm() function. The size() function is used to return the size of each dimension of the specified array "X" or the size of the specified matrix "X". Using Size() and randperm() 1
3 min read
How to Select Random Rows from a Matrix in MATLAB? A matrix is an n x n array that stores integers, floating point numbers or alphanumeric data in MATLAB. Indexing a matrix is the same as indexing an array.  Syntax:matrix_name(i,j)where, i is the row number, and  J is the column number which is to be indexed. Example 1: Matlab % MATLAB code for sel
2 min read
How to Permute the Rows and Columns in a Matrix on MATLAB? In this article, we will discuss how to find the permutation of the rows and columns in a Matrix with the help of multiple approaches Method 1In this approach, we are simply permuting the rows and columns of the matrix in the specified format of rows and columns respectively. Â For column permutation
4 min read
How to swap elements in the matrix in MATLAB? In this article, we will see the swapping of elements into a matrix in MATLAB. Different methods are illustrated below: Method 1: By changing elements of rows and columns  In this method, we are simply changing the elements of particular rows and columns in the specified rows and columns respectivel
3 min read
Turn a Matrix into a Row Vector in MATLAB Conversion of a Matrix into a Row Vector. This conversion can be done using reshape() function along with the Transpose operation. This reshape() function is used to reshape the specified matrix using the given size vector. Syntax:reshape(A, sz) Parameters: This function accepts two parameters, whic
1 min read