Can Matlab randomly generate a combination with groups of varying size?
5 views (last 30 days)
Show older comments
I am quite new with Matlab, is there any buddy have idea how can I generate a combination of numbers with groups of varying size? For example, the number is 1, 2 ,3, 4, 5 ,6, 7 I want to generate a combination with groups like {[1 2],[3 5],[4 6 7]} or {[1],[2],[3],[4],[5],[6],[7]}, or {[1 2 3],[4 5 6],[7]}. Each group can maximumly only have 3 numbers Can any buddy instruct me how to implement this? with great thanks.
0 Comments
Accepted Answer
Stephen23
on 15 Nov 2015
Edited: Stephen23
on 15 Nov 2015
The problem is essentially the random partitioning of a vector. Partitioning is inherently a recursive operation, for which you will find several algorithms online. Typically these algorithms generate all partitions of an integer or set, but you may be able to adapt an algorithm to return just one (random) partition.
A good place to start would be to try out Matt Fig's excellent FEX submission partitions:
You could use Matt Fig's submission to generate all partitions, select those with length three or less, and then randomly select one of those.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!