Shuffling a cell array with constrains

조회 수: 2 (최근 30일)
Varghese
Varghese 2016년 7월 29일
댓글: Varghese 2016년 7월 29일
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일
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개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by