nchoosecrit(S, FUN)

returns those subsets (= combinations of elements of a set) that fulfill a specific criterion
814 descargas
Actualizado 6 feb 2018

Ver licencia

W = nchoosecrit(S, FUN) returns those combinations of one or more element 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.

[W, IX] = nchoosecrit(S, FUN) also returns the indices, such that S(IX{k}) equals W{k}.
Maximally, there are 2^N-1 possible subsets of S (N being the number of elements of S). This number therefore 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 4 or more people that contain either James or Bob,
% but not both!
S = {'Bob' 'Tom' 'Joe' 'Bill' 'James', 'Henry'} ; % the whole group
% criterion 1:
fh1 = @(x) numel(x) >= 4 ;
% 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, IX] = nchoosecrit(S, fhcomb)
S(IX{2}), W{2} % check

Notes:
- If S contain non-unique elements (e.g. S = [1 1 2]), nchoosecrit will
return non-unique cells. In other words, nchoosecrit treats all elements
of S as being unique. One could use nchoosecrit(UNIQUE(S)) to avoid that.
- The output is the same as
Wtemp = nchoose(S) ; W = Wtemp(cellfun(Wtemp, fh)) ;
but does not create the (possible very large) temporary array Wtemp.

See also nchoosek, perms
nchoose, permn, allcomb on the file Exchange

Citar como

Jos (10584) (2024). nchoosecrit(S, FUN) (https://www.mathworks.com/matlabcentral/fileexchange/40301-nchoosecrit-s-fun), MATLAB Central File Exchange. Recuperado .

Compatibilidad con la versión de MATLAB
Se creó con R2017b
Compatible con cualquier versión
Compatibilidad con las plataformas
Windows macOS Linux
Categorías
Más información sobre Argument Definitions en Help Center y MATLAB Answers.
Agradecimientos

Inspirado por: nchoose

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Versión Publicado Notas de la versión
2.0.0.0

included indices as second output

1.0.0.0