RANDSUBSET

Return a random subset of k elements of the set of n elements
401 Downloads
Updated 29 Jun 2010

View License

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!

Cite As

Bruno Luong (2024). RANDSUBSET (https://www.mathworks.com/matlabcentral/fileexchange/28002-randsubset), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Random Number Generation in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.2.0.0

Change the description

1.1.0.0

Mex engine

1.0.0.0