N-back function, same letter repeats more than 4 times

23 次查看(过去 30 天)
function [trial, trial_letters] = my_N_back(trial_count,hit_count,N)
alphabet = ["B","C","D","F","G","H","J","K","L","M","N","P","Q" "R","S","T","V","Y","W","Z"];
false_alarm = zeros(1,trial_count - hit_count);
hit = ones(1,hit_count);
no_hit = 1; %determine where there shall be no hits (first two indices)
max_no_hit_N = max(N,no_hit);
trial = [false_alarm(1:max_no_hit_N),Shuffle([false_alarm(max_no_hit_N+1:end), hit])];
%When false_alarm(1:max_no_hit_N) is called, [0 0 ] is the ans, so the
%first two index, no hits and this is determined by the max_not_hit_N.
trial_letters = strings(1,trial_count);
shuffle_alph = Shuffle(alphabet);
trial_letters(1:max_no_hit_N) = shuffle_alph(1:max_no_hit_N); %determine the first two.
last_letter = '';
count_last_letter = 0;
for i= N+1:length(trial)
if trial(i) == 0
rand_letter = alphabet(randi(numel(alphabet),1,1));
while rand_letter == last_letter
rand_letter = alphabet(randi(numel(alphabet)));
end
last_letter = rand_letter;
count_last_letter = 1;
trial_letters(i) = rand_letter;
else
if trial_letters(i-N) == last_letter
count_last_letter = count_last_letter + 1;
if count_last_letter >= 3
rand_letter = alphabet(randi(numel(alphabet),1,1));
while rand_letter == last_letter
rand_letter = alphabet(randi(numel(alphabet)));
end
last_letter = rand_letter;
count_last_letter = 1;
trial_letters(i) = rand_letter;
else
trial_letters(i) = trial_letters(i-N);
end
else
last_letter = trial_letters(i-N);
count_last_letter = 1;
trial_letters(i) = trial_letters(i-N);
end
end
end
end
>This function is supposed to generate a string array and a binary vector when given the input of trial numbers, hit ratio and the N-back specification.
i.e. With the input of 10 as trial numbers, 5 as hit trials and N as 1: the output could be [A B B B C D D E E E ] the binary vector trial should be
[ 0 0 1 1 0 0 1 0 1 1].
But, the trials and trial_letters do not match. The trial vector has ones, but the trial_letters indicate no hit trial. The function needs to be used with N = 2 and N = 3 as well, and same issue happens with that too. Mostly the mid one, so there are ones in the trial output, where there are not hits in the string array.
  6 个评论
Voss
Voss 2023-2-12
Please give an example of correct output and how you know it's correct.

请先登录,再进行评论。

回答(1 个)

Tridib
Tridib 2025-6-11,13:05
The code does not work as expected because it relies on “last_letter” and “count_last_letter” to manage repetitions which does not align with the core logic of the N-back task. In an N-back task, whether a letter repeats should depend directly on the value of trial(i).
1. For hit trials (trial(i) == 1), “trial_letters(i)” should be set equal to “trial_letters(i-N)”.
2. For non-hit trials (trial(i) == 0), a new letter should be chosen that is different from “trial_letters(i-N)” to avoid accidental matches. Also, avoid repeating the same letter too many times in a row.
3. Remove “last_letter” and “count_last_letter” and replace the current for loop with the following code that checks trial(i) directly and assigns letters accordingly.
% filling letters based on the trial binary vector
for i = max_no_hit_N + 1 : trial_count
if trial(i) == 1
% Hit: should repeat the letter from N-back
trial_letters(i) = trial_letters(i - N);
else
% Non-hit: pick a new random letter different from i-N
rand_letter = alphabet(randi(numel(alphabet), 1, 1));
while any(i > N & rand_letter == trial_letters(i - N)) || ...
(i >= 2 && rand_letter == trial_letters(i - 1)) % avoiding consecutive repeats
rand_letter = alphabet(randi(numel(alphabet), 1, 1));
end
trial_letters(i) = rand_letter;
end
end
end
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Psychology 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by