allcomb(varargin)

All combinations of input (v4.2, apr 2018)
25,2K Downloads
Aktualisiert 30. Apr 2018

Lizenz anzeigen

Anmerkung des Herausgebers: This file was a File Exchange Pick of the Week

ALLCOMB - All combinations
B = ALLCOMB(A1,A2,A3,...,AN) returns all combinations of the elements
in the arrays A1, A2, ..., and AN. B is P-by-N matrix, where P is the product
of the number of elements of the N inputs. This functionality is also
known as the Cartesian Product. The arguments can be numerical and/or
characters, or they can be cell arrays.

Examples:
allcomb([1 3 5],[-3 8],[0 1]) % numerical input:
% -> [ 1 -3 0
% 1 -3 1
% 1 8 0
% ...
% 5 -3 1
% 5 8 1 ] ; % a 12-by-3 array

allcomb('abc','XY') % character arrays
% -> [ aX ; aY ; bX ; bY ; cX ; cY] % a 6-by-2 character array

allcomb('xy',[65 66]) % a combination
% -> ['xA' ; 'xB' ; 'yA' ; 'yB'] % a 4-by-2 character array

allcomb({'hello','Bye'},{'Joe', 10:12},{99999 []}) % all cell arrays
% -> { 'hello' 'Joe' [99999]
% 'hello' 'Joe' []
% 'hello' [1x3 double] [99999]
% 'hello' [1x3 double] []
% 'Bye' 'Joe' [99999]
% 'Bye' 'Joe' []
% 'Bye' [1x3 double] [99999]
% 'Bye' [1x3 double] [] } ; % a 8-by-3 cell array

ALLCOMB(..., 'matlab') causes the first column to change fastest which
is consistent with matlab indexing. Example:
allcomb(1:2,3:4,5:6,'matlab')
% -> [ 1 3 5 ; 1 4 5 ; 1 3 6 ; ... ; 2 4 6 ]

If one of the arguments is empty, ALLCOMB returns a 0-by-N empty array.

See also nchoosek, perms, ndgrid
and nchoose, combn, kthcombn (matlab Central FEX)

Zitieren als

Jos (10584) (2024). allcomb(varargin) (https://www.mathworks.com/matlabcentral/fileexchange/10064-allcomb-varargin), MATLAB Central File Exchange. Abgerufen.

Kompatibilität der MATLAB-Version
Erstellt mit R2015a
Kompatibel mit allen Versionen
Plattform-Kompatibilität
Windows macOS Linux
Kategorien
Mehr zu Argument Definitions finden Sie in Help Center und MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Veröffentlicht Versionshinweise
4.2.0.0

fixed some grammar mistakes

4.1.0.0

fixed error noted by Richard

1.6.0.0

v4.0 accepts cell arrays as input

1.5.0.0

now accepts character arrays

1.4.0.0

remove superfluous line of code (thanks to Edward)

1.3.0.0

(2.1, feb 2011) - fixed the check of the last argument when the last argument was neither a char or a double.

1.2.0.0

Convinced by Bruno, I decided to change the behavior when an input is empty (now returns empty instead of ignoring the input).

1.1.0.0

little speed improvement as suggested by Jan Simon

1.0.0.0

1) Removed a erroneous warning when no empty inputs were _not_ provided.
2) Added the option to let the first input run fastest (thanks to John D'Errico)