Can Matlab randomly generate a combination with groups of varying size?

4 views (last 30 days)
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.

Accepted Answer

Stephen23
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.
  1 Comment
Ben
Ben on 15 Nov 2015
Hi Stephen,
this definitely helps. However, I have came up another solution, I can use randperm to generate a combination, and then I can randomly generate a group number between ceil(m/3) to m, say if I have m numbers. Then I can randomly generate bits of number, whose count is the same as group number. Thanks however.

Sign in to comment.

More Answers (0)

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!