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

5 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2015-11-15
编辑:Stephen23 2015-11-15
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 个评论
Ben
Ben 2015-11-15
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by