RANDSUBSET
Version 1.2.0.0 (4.41 KB) by
Bruno Luong
Return a random subset of k elements of the set of n elements
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 (2026). 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 LinuxCategories
Find more on Random Number Generation in Help Center and MATLAB Answers
Tags
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
