k indexes from 1:n

Return matrix of all possible k indexes, it is C(n,k) variants
310 Downloads
Updated 24 Sep 2010

View License

http://simulations.narod.ru/
Using:
ind=all_combinations(n,k)
Number of all posible variants is nv=C(n,k)=n!/(k!*(n-k)!) (binomial coefficient)
ind is nv-by-k matrix. Example, 2 indexes from 1 2 3:
all_combinations(3,2)

ans =

1 2
1 3
2 3

Example 3 from 5:
all_combinations(5,3)

ans =

1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

Cite As

Maxim Vedenyov (2024). k indexes from 1:n (https://www.mathworks.com/matlabcentral/fileexchange/28818-k-indexes-from-1-n), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Matrix Indexing in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0.0