Home > utility > combine.m

combine

PURPOSE ^

out = combine(obj, n) returns combinations of obj with n distinct

SYNOPSIS ^

function out = combine(obj, n)

DESCRIPTION ^

    out = combine(obj, n) returns combinations of obj with n distinct
    elements.
    For instance: combine([1 2 3 4 5], 2) or combine('abcde', 3).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function out = combine(obj, n)
0002 %    out = combine(obj, n) returns combinations of obj with n distinct
0003 %    elements.
0004 %    For instance: combine([1 2 3 4 5], 2) or combine('abcde', 3).
0005 
0006 %    Roger Jang, Sept-21-1996
0007 
0008 if n>length(obj)
0009     out=[];
0010     return;
0011 end
0012 if n==1
0013     out = obj(:);
0014     return;
0015 end
0016 if n==length(obj)
0017     out = obj(:)';
0018     return;
0019 end
0020 
0021 out = [];
0022 for i = 1:length(obj)-1,
0023     first = obj(i);
0024     tail = obj(i+1:end);
0025     tail_combinat = combine(tail, n-1);
0026     loop_out = [first*ones(size(tail_combinat,1), 1), tail_combinat]; 
0027     out = [out; loop_out];
0028 end

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003