Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random order of rows Matlab

Tags:

shuffle

matlab

Say we have a matrix of size 100x3

How would you shuffle the rows in MATLAB?

like image 822
edgarmtze Avatar asked Mar 26 '11 18:03

edgarmtze


People also ask

How do you randomly shuffle rows in MATLAB?

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.

How do you sort randomly in MATLAB?

To sort the elements of a vector randomly you can use the RANDPERM() function. RANDPERM(n) returns a random permutation of the integers 1:n.

How do I reorder rows in MATLAB?

Description. B = sortrows( A ) sorts the rows of a matrix in ascending order based on the elements in the first column. When the first column contains repeated elements, sortrows sorts according to the values in the next column and repeats this behavior for succeeding equal values.

How do you shuffle the rows of a matrix?

You can use numpy. random. shuffle() . This function only shuffles the array along the first axis of a multi-dimensional array.


2 Answers

To shuffle the rows of a matrix, you can use RANDPERM

shuffledArray = orderedArray(randperm(size(orderedArray,1)),:); 

randperm will generate a list of N random values and sort them, returning the second output of sort as result.

like image 136
Jonas Avatar answered Sep 28 '22 11:09

Jonas


This can be done by creating a new random index for the matrix rows via Matlab's randsample function.

matrix=matrix(randsample(1:length(matrix),length(matrix)),:); 
like image 23
KnowledgeBone Avatar answered Sep 28 '22 10:09

KnowledgeBone