Shuffling a cell array with constrains

I created a cell array with 14 elements repeated 11 times. Now I need to shuffle the array with a constraint that the elements should not repeat in succession (ie, two 'bun' should not come together)
Stimuli={'bun','bin','din','gun','gin','kun','kin','pun','pin','ʃun','ʃin','sun','tun','tin'}; B=repmat (Stimuli,1,11)
Thank you, Varghese

 採用された回答

Guillaume
Guillaume 2016 年 7 月 29 日
編集済み: Guillaume 2016 年 7 月 29 日

0 投票

A brute force approach would work:
while true
Stimuli = Stimuli(randperm(numel(Stimuli))); %shuffle randomly, may contain repetitions
if ~any(strcmp(Stimuli(1:end-1), Stimuli(2:end))) %check for two consecutive identical strings
break; %no identical consecutive strings, exit loop
end
end

1 件のコメント

Varghese
Varghese 2016 年 7 月 29 日
Thank you Guillaume !! It worked !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

製品

質問済み:

2016 年 7 月 29 日

コメント済み:

2016 年 7 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by