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

5 views (last 30 days)
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!

Answers (1)

Dave Behera
Dave Behera on 6 Apr 2016
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.

Categories

Find more on Characters and Strings 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!