RANDPERM: Randomly choosing a subset of values in Matlab

This is an important functionality I use in my simulations but when I want it I always have to search for it because I forget the Matlab function name. There are lots of loopy ways using random numbers to get this done, but there is an in-built Matlab function you can use: randperm(n).

I often need to choose a set of values from a vector. For example, randomly choosing a set of users. Random numbers cannot be used because they can give you identical numbers. Need a method that would generate different numbers. What randperm does is scrambling a sequence of numbers.

For example;

randperm(5) = 3 1 4 2 5

So if you wanted to choose 3 values randomly from a set of 5, simply use the values in the indexes given by the first 3 values returned by randperm(5).

Post to Twitter Tweet This Post



Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes