No BSD License  

Highlights from
pickn

from pickn by Richard Medlock
Return P random picks of N items from an input vector A.

pickn(A,N,P)
function B = pickn(A,N,P)

% B = PICKN(A,N,P)
% 
% Returns P random Picks of N items from vector A.
% 
% PickN assumes that each item can be repelaced after
% selection.
% 
% Example:
%   A list contains four elements: [1 2 3 4]
%   Show two possible combinations of choosing 3 items
%   from the list.
% 
% 
% A = [1:4];
% N = 3;
% P = 2;
% 
% B = pickn(A,N,P)
% 
%       1   3   1
%       2   4   1
% 
% 
% If you specifcy P as the string 'all', then B will contain
% all possible permutations.
%
%
% Richard Medlock, 9-Apr-2003.



% For debug:
% A = [1:4];                  % Input Vector, A
L = length(A);              % Number of Elements in A
% N = 3;                      % Number of Picks (and Number of Columns)
R = L^N;                    % Total Number Of Picks Possible (Rows)
for c = 1:N                 % For Each Column...
    
    cpys = L^(c-1);         % Number of Copies of Each Element in A.
    vsize = cpys*L;         % Size of Vector
    
    for e = 1:cpys          % for each element
        
        counter = 0;        % initialize a counter.
        
        % output index in steps of cpys until the output vector is filled.
        for i = e:cpys:vsize; 

            counter = counter + 1;
            v(i) = A(counter);
            
        end
    end
    
    % Number of repeats of v to fill R...
    M = R/vsize;
    
    % Repeat the matrix...
    B(:,c) = repmat(v',M,1);
    
end

B = fliplr(B);              % Put into a nicer order for viewing.

switch P                    % See how many selections the user wants.
    
    case 'all'              % If they want to see all...
        % don't do anything - just return all of B.                    
        
    otherwise
                            % Generate P random numbers where 1 <= P <= R 
        p = round(1 + (R-1) * rand(P,1));
        B = B(p,:);         % Return those picks.
end

Contact us at files@mathworks.com