Code covered by the BSD License

### Highlights from NCHOOSECRIT (V1.0 feb 2013)

5.0

5.0 | 2 ratings Rate this file 8 Downloads (last 30 days) File Size: 2.68 KB File ID: #40301

# NCHOOSECRIT (V1.0 feb 2013)

returns those subsets (= combinations of elements of a set) that fulfill a specific criterion

File Information
Description

NCHOOSECRIT - subsets (combinations of elements of a set) that fulfill a criterion

W = NCHOOSECRIT(S, FUN) returns those combinations of one or more elements of the set S (called a subset) that fulfill a specific criterion.
This criterion is specified by the function FUN. FUN is a function handle to a function that takes one input argument and returns a logical scalar value.
W will be cell array of row vectors. Each cell of W holds one of the combinations C of S for which FH(C) is true.

In total, there are 2^N-1 possible subsets of S (N being the number of elements of S). This number grows rapidly with increasing N. W is a selection of those subsets.

S can be a cell array, and each cell of W will then contain a cell array.

Examples:
% find the subsets that sum op to 6
nchoosecrit([1 2 3 4 5 6], @(x) sum(x)==6)
% -> { [1 2 3], [2 4], [1 5], [6]}

% find subgroups of 3 or more people that contain either James or Bob,
% but not both!
S = {'Bob' 'Tom' 'Joe' 'Bill' 'James'} ; % the whole group
% criterion 1:
fh1 = @(x) numel(x) >= 3 ;
% criterion 2
fhname = @(x,y) any(strncmp(y,x,numel(y))) ;
fh2 = @(x) xor(fhname(x,'James'), fhname(x,'Bob')) ;
% the 2 criterions combined:
fhcomb = @(x) fh1(x) && fh2(x) ;
W = nchoosecrit(S, fhcomb)

nchoose, combn, allcomb on the File Exchange

Acknowledgements

Nchoose (V2.1 Mar 2010) inspired this file.

MATLAB release MATLAB 7.12 (R2011a)
Other requirements should work in most releases
30 Aug 2013

Wonderful!

16 Apr 2013