Code covered by the BSD License  

Highlights from
RANDSUBSET

Be the first to rate this file! 0 Downloads (last 30 days) File Size: 4.41 KB File ID: #28002

RANDSUBSET

by Bruno Luong

 

23 Jun 2010 (Updated 29 Jun 2010)

Return a random subset of k elements of the set of n elements

| Watch this File

File Information
Description

A = RANDSUBSET(N, K) is equivalent to

ALLSUBSETS = NCHOOSEK(1:N,K);
A = ALLSUBSETS(RANDI(NCHOOSEK(N,K)));

% or

A = RANDPERM(N);
A = SORT(A(1:k));

This function can also be used to generate random sampling without replacement by calling RANDPERM on the output.

It works advantageously with large N (but "reasonable" K), and fast!

MATLAB release MATLAB 7.10 (2010a)
Other requirements Mex correctly setup
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
25 Jun 2010 Bruno Luong

For the moment, it is not recommended using this function for k larger than 1e4, since the insertion complexity is O(k^2), which is not optimal. I'll work for the better insertion algorithm for the next release.

Please login to add a comment or rating.
Updates
24 Jun 2010

Mex engine

29 Jun 2010

Change the description

Tag Activity for this File
Tag Applied By Date/Time
randperm Bruno Luong 24 Jun 2010 10:57:39
subset Bruno Luong 24 Jun 2010 10:57:39
random Bruno Luong 24 Jun 2010 10:57:39
permutation Bruno Luong 24 Jun 2010 10:57:40
without replacement Bruno Luong 24 Jun 2010 10:57:40

Contact us at files@mathworks.com