Permutation of array elements

Hi, I have 3 array with different number of cells and a matrix of 1 row and 3 columns composed by the arrays.
ACTIVITY_WAKE = {'awake_in_bed','out_of_bed','out_bedroom'};
ACTIVITY_MORNING_TOILET = {'enter_toilet','sit_WC','wash','exit_toilet'};
ACTIVITY_BREAKFAST = {'prepare_breakfast','have_breakfast','wash_dishes'};
WAKE_UP = [ACTIVITY_WAKE ACTIVITY_MORNING_TOILET ACTIVITY_BREAKFAST];
I would like to generate WAKE_UP with random order, but when I use randperm command I have permutation of the element in single array, I only need to permute the array, like:
WAKE_UP = [ACTIVITY_WAKE ACTIVITY_BREAKFAST ACTIVITY_MORNING_TOILET] or
WAKE_UP = [ACTIVITY_MORNING_TOILET ACTIVITY_WAKE ACTIVITY_BREAKFAST ]

 Accepted Answer

This seems to work
A = {'test a 1', 'test a 2'};
B = {'test b 1', 'test b 2'};
WAKE = [A',B'] %see below for explanation
WAKE(:, randperm(2))
If we simply write WAKE = [A,B], the vector will become
'test a 1' 'test a 2' 'test b 1' 'test b 2'
instead I write transpose the A and B elements, so WAKE = [A',B'] which gives
'test a 1' 'test b 1'
'test a 2' 'test b 2'
This should work in a similar way in your case, but I have not tested this.

7 Comments

Yes, but I have always the same order
Nevermind, the solution above will only work if length(A)==length(B), but it seems that you want to have different lengths.
I don't understand, in this way WAKE_UP has always the same order...

Yes, I have different lengths, for example: awake_in_bed = [1 5*60 1*60]; out_of_bed = [3 30 10]; out_bedroom = [2 2*60 15]; ACTIVITY_WAKE = {'awake_in_bed','out_of_bed','out_bedroom'}; enter_toilet = [2 20 5]; sit_WC = [3 5*60 30]; exit_toilet = [2 20 5]; wash = [2 15*60 2*60]; ACTIVITY_MORNING_TOILET = {'enter_toilet','sit_WC','wash','exit_toilet'}; prepare_breakfast = [2 2*60 30]; have_breakfast = [3 30*60 2*60]; wash_dishes = [2 5*60 60]; ACTIVITY_BREAKFAST = {'prepare_breakfast','have_breakfast','wash_dishes'};

and after the definition of these features, I have:

WAKE_UP = [ACTIVITY_WAKE ACTIVITY_MORNING_TOILET ACTIVITY_BREAKFAST];

Ok, if I understand you correctly you want to randomize the order of the activites in blocks and not the order of the activites themselves.
I have had some more time to think about it. I believe this is what you want:
ACTIVITY_WAKE = {'awake_in_bed','out_of_bed','out_bedroom'};
ACTIVITY_MORNING_TOILET = {'enter_toilet','sit_WC','wash','exit_toilet'};
ACTIVITY_BREAKFAST = {'prepare_breakfast','have_breakfast','wash_dishes'};
activities = {'ACTIVITY_WAKE', 'ACTIVITY_MORNING_TOILET', 'ACTIVITY_BREAKFAST'};
WAKE = randperm(3);
ff = cellfun(@eval, activities(WAKE),'UniformOutput', false);
[ff{:}]'
Just rerun the randperm to get new random orders.
Would there be a way to prevent repeats of the same combination?
@Sam, please open a new question.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!