File Exchange

image thumbnail

NCHOOSECRIT (V1.0 feb 2013)

version 1.0 (2.68 KB) by

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

7 Downloads

Updated

View License

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)

See also nchoosek, perms
nchoose, combn, allcomb on the File Exchange

Comments and Ratings (2)

Marin

Marin (view profile)

Wonderful!

MATLAB Release
MATLAB 7.12 (R2011a)
Acknowledgements

Inspired by: NCHOOSE

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video