image thumbnail

combs: Generate all possible combinations of vector elements

version 1.0.0 (1.34 KB) by Carlos Souto
Generate all possible combinations of the elements of a given vector.

2 Downloads

Updated 22 Sep 2021

View License

Generate all possible combinations of the elements of a given vector.
table = combs(v, k) returns a matrix containing all possible combinations of the elements of vector v selected k times (includes repetitions). Matrix table has k columns and n^k rows, where n is the number of elements of vector v.
>> combs([-1, 0, 1], 3)
ans =
-1 -1 -1
-1 -1 0
-1 -1 1
-1 0 -1
-1 0 0
-1 0 1
-1 1 -1
-1 1 0
-1 1 1
0 -1 -1
0 -1 0
0 -1 1
0 0 -1
0 0 0
0 0 1
0 1 -1
0 1 0
0 1 1
1 -1 -1
1 -1 0
1 -1 1
1 0 -1
1 0 0
1 0 1
1 1 -1
1 1 0
1 1 1

Cite As

Carlos Souto (2021). combs: Generate all possible combinations of vector elements (https://www.mathworks.com/matlabcentral/fileexchange/99684-combs-generate-all-possible-combinations-of-vector-elements), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2021a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!