Permutation of array elements

14 ビュー (過去 30 日間)
Andrea
Andrea 2015 年 1 月 28 日
編集済み: Walter Roberson 2018 年 8 月 21 日
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 日
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 件のコメント
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 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by