0

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.

My stimulus file (stim.txt) has a column "items" with the stimuli that looks like:

practice1

practice 2

word1

word2

word3

word4

word5

word1

word2

word3

word4

word5

.... x other 11 times (only the testing stimuli are repeated) ....

Here is the part of interest of my 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); 

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.

I want to randomize the testing trials without having the same verbal stimulus repeated consecutively.

Can I ask for your help? Thank you very very much!! :)

chiaras15
  • 3
  • 2
  • Could you edit the question to add more detail? What is it you're trying to do, what have you tried, and what is your code actually doing? – Dan Pollard Feb 12 '21 at 12:50
  • Does Matlab have a `shuffle` function? That is probably what you are looking for. – rossum Feb 12 '21 at 15:42
  • I edited the question, thank you! Hope it's more clear now... Yes, there is a `shuffle` function but the results is the same as "randperm", trials in random order but the same item is repeated more times consecutively. – chiaras15 Feb 12 '21 at 19:12

1 Answers1

0

One way to generate a pseudorandom sequence as you have described, is to randomly select on each trial, a word that doesn't equal the previous trial. An example is below (called 'Method 1'). For clarity, the values randomized are the numbers 1-5, corresponding to the 5 words, rather than the positions within the TrialIndex as above.

One potential issue with this pseudo-randomization is that, unlike with a shuffle, you aren't guaranteed to have equal proportions of the 5 words presented across the whole experiment, as words are selected randomly on each trial.

A different method, inspired by this answer in Python, https://stackoverflow.com/a/52556609/2205580 , is to shuffle and append subsequences. This method generates equal number of each shuffled value but has the downside of reduced randomness. Specifically, since values are shuffled in blocks, the next occurrence of a word will be somewhat predictable, this there is a limitation on how soon after a presentation of a word that it will be presented again. This is shown below as 'Method 2'

% generating sequence
num_words = 5;
word_options = 1:num_words;
num_repeats = 13;
num_trials = num_words * num_repeats;


% Method 1

word_order = nan(1, num_trials);

% randomly choose a word (1-5) for the initial trial
word_order(1) = randi(5);

% for each subsequent trial, randomly choose a word that isn't a repeat
for k = 2:num_trials
    word_order(k) = RandSel(setdiff(word_options, word_order(k-1)), 1);
end

% diagnostics on sequence
disp('Word Order:')
disp(word_order);
% verify there are no repeated elements in the sequenceas
has_repeats = any(diff(word_order) == 0);
if (has_repeats)
    disp('sequence has sequential repeats!')
else
    disp('sequence has no sequential repeats!')
end

for k = 1:num_words
   fprintf('Word %i is present at a rate of %2.2f \n', k, mean(word_order == k)); 
end


% Method 2

word_order = nan(1, num_trials);

for k = 1:num_words:num_trials
    subsequence = Shuffle(word_options);
    word_order(k:k+(num_words-1)) = subsequence;
    % swap the first value of this subsequence if it repeats the previous
    if k > 1 && (word_order(k) == word_order(k-1))
        word_order([k, k+1]) = word_order([k+1, k]);
    end
end

% diagnostics on sequence
disp('Word Order:')
disp(word_order);
% verify there are no repeated elements in the sequenceas
has_repeats = any(diff(word_order) == 0);
if (has_repeats)
    disp('sequence has sequential repeats!')
else
    disp('sequence has no sequential repeats!')
end

for k = 1:num_words
   fprintf('Word %i is present at a rate of %2.2f \n', k, mean(word_order == k)); 
end
DMR
  • 1,479
  • 1
  • 8
  • 11