shuffle matrix element in matlab

I have a martix and want to shuffle element of it .

x=[1 2 5 4 6 ]

after shuffle(something like this)

x=[2 4 6 5 1]    

is matlab has function for it ? in php array_shuffle do this.

Answers


  1. obtain shuffled indices using randperm

    idx = randperm(length(x));
    
  2. use indices to obtain shuffled vector

    xperm = x(idx);


As an alternate to randperm, you can also use randsample from the statistics toolbox.

y = randsample(n,k) returns a k-by-1 vector y of values sampled uniformly at random, without replacement, from the integers 1 to n.

Note that it is "without replacement" (by default). So if you set k as length(x), it is equivalent to doing a random shuffle of the vector. For example:

x = 1:5;
randsample(x,length(x))
%ans = 
%       4     5     3     1     2

I like this more than randperm, because it is easily extensible to different uses. For example, to draw 3 elements from x at random (like drawing from a bucket with finite items), you do randsample(x,3). Likewise, if you wish to draw 3 numbers, where the alphabet is made up of the elements of x, but allow for repetitions, you do randsample(x,3,true).


Need Your Help

How to show InfoWindow using Android Maps Utility Library for Android

android google-maps markerclusterer

I'm using Google Maps Android API Utility Library in order to show several markers in a map in a clustered way.

Android Studio Gradle: Error:Execution failed for task ':app:processDebugGoogleServices'. > No matching client found for package

android android-studio android-gradle google-play-services build.gradle

I am trying to upgrade my google play services dependencies to 8.4.0 by following the example Google gives here, but I am getting the following error ('com.example.exampleapp' is a replacement for ...