1D array combinations
Show older comments
Hi all,
I have a 1D array containing numbers from 1 to 5.
A = [1
2
3
4
5]
How can I find all possible combinations (without doubles), so that...
B = [1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
2
2 3
etc.]
I tried a number of combinators from the FEX but they don't give me the result I'm after.
Thanks!
1 Comment
KSSV
on 23 Feb 2016
are you looking for something like the function perms?
doc perms
Accepted Answer
More Answers (1)
Jos (10584)
on 23 Feb 2016
Edited: Jos (10584)
on 23 Feb 2016
My function NCHOOSE will create those combinations without (slowly) looping over nchoosek. Each combination is stored in a cell. The cell array can be easily transformed into a 2D array by padding the entries with NaNs using PADCAT.
C = nchoose(1:5)
A = padcat(C{:})
NCHOOSE and PADCAT can be downloaded here:
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!