title: Matlab: How to randomize trials without presenting the same trial consecutively
body: <p>I'm a very beginner of Matlab and I'm struggling in randomizing trials in my task on Psychtoolbox, in Matlab. My trials consist in 5 verbal stimuli repeated 12 times (in total 60 trials). I have 2 practice trials and then the 60 testing trials. What I would like to do is to present the practice trial in a fixed order, and the testing trials in a random order, without having the same verbal stimulus repeated consecutively.</p>
  <p>My stimulus file (stim.txt) has a column &quot;items&quot; with the stimuli that looks like:</p>
  <p>practice1</p>
  <p>practice 2</p>
  <p>word1</p>
  <p>word2</p>
  <p>word3</p>
  <p>word4</p>
  <p>word5</p>
  <p>word1</p>
  <p>word2</p>
  <p>word3</p>
  <p>word4</p>
  <p>word5</p>
  <p>.... x other 11 times (only the testing stimuli are repeated) ....</p>
  <p>Here is the part of interest of my code:</p>
  <pre><code>%here I define the trial info
  trial = 1;
  maxTrial = length(stim.items); %this is the excel file from which I take the stimuli 
  
  % I define the number of practice trials 
  NumOfPracticeTrials=2;
  
  %I randomize only the testing trials 
  TrialIndex(NumOfPracticeTrials+1:maxTrial)=NumOfPracticeTrials+randperm((maxTrial-NumOfPracticeTrials); 
  </code></pre>
  <p>This code works, so that I present the practice trials in a fixed order, while the testing trials are randomized. However, when I run the experiment, some testing verbal stimuli are repeated twice or even more times consecutively.</p>
  <p>I want to randomize the testing trials without having the same verbal stimulus repeated consecutively.</p>
  <p>Can I ask for your help? Thank you very very much!! :)</p>