create random-order list with items not repeating within 8 occurences

1 回表示 (過去 30 日間)
Darya Frank
Darya Frank 2016 年 4 月 4 日
回答済み: Dave Behera 2016 年 4 月 6 日
I'm quite new to Matlab and any help/ideas would be much appreciated! I have a list of 240 strings in a cell that look like:
1a b
1a f
1b f
1b b
1c f
1c b
1d f
1d b
2a b
2a f
etc. until 30d...
I'm trying to re-order this list such that there's at least 8 occurrences between 1X (x=a,b,c,d) and any other 1X, with each occurring only once.
For example:
1a
5d
8c
29b
27a
12a
30b
9c
1d
Does anyone have any idea where to start? I've tried using randperm and a custom-built randswap functions but I can't seem to figure out how to make it fulfil the min-8 occurrences condition
Many thanks!

回答 (1 件)

Dave Behera
Dave Behera 2016 年 4 月 6 日
You could use this approach:
1. Create a empty cell array A
A = {}
2. Go through the list looking for 1_x strings (using for loop). Every time one is found, create a new cell array beginning with that string. Add this new cell array to the array A. In the end, A should look like: { {1_a}, {1_b},.....}
3. Go through the list again, this time looking for non- 1_x strings. Add each such string to a different cell array in A, so that each cell array for a 1_x string gets populated. One way would be to add the 1st string to A{1}, .ie {1_a} in example above, 2nd string to {1_b}, etc. When end of A is reached, add the string to A{1} again and repeat.
In the end, A should look like:
{ {1_a, 5_d, 6_c.....}, {1_b, 7_g,......},.....}
4. Then, concatenate all the cell arrays inside A into a single cell array, which will be the final answer.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by