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

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 Comments

Please provide the values of the inputs (trial_count,hit_count,N) for which the function gives different outputs (trial, trial_letters) than you would like (at least sometimes), and please describe what outputs you would like it to give in that case and why.
It will also help if you describe what you want this function to do and how it's supposed to do that.
Also please provide code for the missing function Shuffle, if it is different than the following:
function x = Shuffle(x)
x = x(randperm(numel(x)));
end
I have no idea if the following is a good change to the code or not, since I don't know what the code's supposed to be doing, but last_letter is only updated when trial(i) == 0. Should it perhaps be updated each time through the loop? i.e.:
last_letter = "";
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
trial_letters(i) = rand_letter;
else
trial_letters(i) = trial_letters(i-N);
end
last_letter = trial_letters(i);
end
I apologize for the lack of info, I updated my question now, and The Shuffle function was a in-built function for me, but it randomizes the sequence. Could be the last letter as well, sometimes it generates that too, ´with different number of trials. Its almost 90% percent correct but there is a shift that I cant figure out.
"The trial vector has ones, but the trial_letters indicate no hit trial."
Please indicate how you know that.
I manually checked and compared
Please give an example of correct output and how you know it's correct.

Sign in to comment.

Answers (1)

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!

Categories

Find more on Psychology in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 10 Feb 2023

Answered:

on 11 Jun 2025

Community Treasure Hunt

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

Start Hunting!