MATLAB: Can Matlab randomly generate a combination with groups of varying size

random number generator

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.

Best Answer

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.