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 ]

 채택된 답변

Matz Johansson Bergström
Matz Johansson Bergström 2015년 1월 28일
편집: Matz Johansson Bergström 2015년 1월 28일

0 개 추천

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

Andrea
Andrea 2015년 1월 28일
Yes, but I have always the same order
Matz Johansson Bergström
Matz Johansson Bergström 2015년 1월 28일
Nevermind, the solution above will only work if length(A)==length(B), but it seems that you want to have different lengths.
Andrea
Andrea 2015년 1월 28일
I don't understand, in this way WAKE_UP has always the same order...
Andrea
Andrea 2015년 1월 28일

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.
Sam Ferguson
Sam Ferguson 2018년 8월 21일
Would there be a way to prevent repeats of the same combination?
Paolo
Paolo 2018년 8월 21일
@Sam, please open a new question.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by