首页 > 解决方案 > Matlab:如何在不连续呈现相同试验的情况下随机化试验

问题描述

我是 Matlab 的初学者,我在 Matlab 的 Psychtoolbox 上的任务中努力随机试验。我的试验包括重复 12 次的 5 种语言刺激(总共 60 次试验)。我有 2 个练习试验,然后是 60 个测试试验。我想做的是以固定的顺序呈现练习试验,以随机顺序呈现测试试验,而不是连续重复相同的口头刺激。

我的刺激文件(stim.txt)有一列“项目”,其中的刺激看起来像:

练习1

练习 2

单词1

字2

词3

字4

字5

单词1

字2

词3

字4

字5

.... x 其他 11 次(仅重复测试刺激) ....

这是我的代码感兴趣的部分:

%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); 

此代码有效,因此我以固定顺序呈现练习试验,而测试试验是随机的。然而,当我进行实验时,一些测试性语言刺激会连续重复两次甚至更多次。

我想在没有连续重复相同的口头刺激的情况下随机化测试试验。

我可以请求你的帮助吗?非常非常感谢你!!:)

标签: matlabrandompsychtoolbox

解决方案


如您所述,生成伪随机序列的一种方法是在每次试验中随机选择一个不等于先前试验的单词。下面是一个示例(称为“方法 1”)。为清楚起见,随机化的值是数字 1-5,对应于 5 个单词,而不是上面 TrialIndex 中的位置。

这种伪随机化的一个潜在问题是,与随机播放不同,您不能保证在整个实验中呈现相同比例的 5 个单词,因为单词是在每次试验中随机选择的。

受 Python 中的这个答案https://stackoverflow.com/a/52556609/2205580启发,另一种方法是随机播放和附加子序列。此方法生成相同数量的每个混洗值,但具有降低随机性的缺点。具体来说,由于值是在块中打乱的,单词的下一次出现将在某种程度上是可预测的,这对在呈现单词后多久再次呈现单词存在限制。这在下面显示为“方法 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

推荐阅读