VOOZH about

URL: https://www.geeksforgeeks.org/software-engineering/how-can-be-randomly-shuffle-rows-in-matlab-matrix/

⇱ How Can Be Randomly Shuffle Rows in MATLAB Matrix? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How Can Be Randomly Shuffle Rows in MATLAB Matrix?

Last Updated : 2 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 

Output

B =
 1 2 3
 7 8 9
 4 5 6

Example 2

Output

ans =
 9 10 11 12
 1 2 3 4
 5 6 7 8
 13 14 15 16
Comment

Explore