How can I control the amount of same random letters back to back? (N-back)

5 visualizaciones (últimos 30 días)
Hey,
I am designing an n-back experiment for my project (on psychtoolbox) and I have troubles controlling the letters that needed to be presented back to back. My projects demands that there should be certain amount of same letters. For instance, if there are 50 trials (1 trial = 1 letter) 5 times there should same letters back to back (avoiding the first 2 trials) and the letters should be random for each time the code is run.
So far what I have is this;
alphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','R','S','T','U','V','Y','Z','W','Q'};
num_shuffled = randi([1 25], [1 50])
alphabet_new = alphabet(num_shuffled);
for checked = 1:49
if alphabet_new{checked} == alphabet_new{checked+1}
new_num = randi([1 25], 1)
alphabet_new{checked} = alphabet_new{new_num}
elseif alphabet_new{checked} ~= alphabet_new{checked+1}
alphabet_new{1,checked} = alphabet_new{1,checked}
end
end
%This part is to control for 5 trials just to try it out.
integ =[];
for cond = 1:5
integ(1,cond)=randi([2 48]);
alphabet_new(integ(cond)) = alphabet_new(integ(cond)+1);
end
This works, but not always. Sometimes I end up with more than 5 times or less than 5 times. So I am wondering If there is a more efficient way to do it?
I'd really appreciate any ideas!
Thanks.

Respuesta aceptada

Karim
Karim el 21 de Jun. de 2022
Quite an interesting problem, I'm not familiar with a n-back experiment. But I guess one of the issues could be that your 'integ' indexes are not guarenteed to be unique values. Also you only loop ones over the inital shuffeling, so again it could be that after the shuffeling you still end up with repeated values.
Below I tried to modify the code so that it (hopfully) produces the output that you want.
Essentially it picks 'testsize' characters, and keeps on shuffeling them until it obtains a string with non repeated random characters. Afterwards if picks 'trial_value' locations to introduce the repeated character.
alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q" "R","S","T","U","V","X","Y","W","Z"];
TestSize = 50;
trial_value = 5;
% generate a string with "testsize" characters
num_shuffled = randi(numel(alphabet),1,TestSize);
% make sure there are no repeated values...
while true
% test if integers are repeated
change_me = [false diff(num_shuffled)==0];
% if not end the loop
if all(~change_me)
break
end
% if so pick new integers
num_shuffled(change_me) = randi(numel(alphabet),1,sum(change_me));
end
% create the new string
alphabet_new = alphabet(num_shuffled);
% now make sure that we have "trial_value" repeated characters (at unique locations)
trial_idx = sort(1 + randperm((TestSize-1),trial_value));
alphabet_new(trial_idx) = alphabet_new(trial_idx+1);
% print the string to the display
fprintf('Current test string is: %s \n',join(alphabet_new))
Current test string is: C V V N D E E M T U W I Z O R A Q E E D S E X O V R G B N E I B N F Q L B J S D H L L A M C Y A T B
fprintf('Trial indexes are: %i %i %i %i %i \n',trial_idx)
Trial indexes are: 2 6 17 18 42
fprintf('Trial characters are: %s \n',join(alphabet_new(trial_idx)))
Trial characters are: V E Q E L
  1 comentario
Selen Soylu
Selen Soylu el 21 de Jun. de 2022
This works pretty smoothly, thank you very much. It also allows me to flexibly alter the code according to the target trials and from 1-back to 2-back! Thanks really

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Timing and presenting 2D and 3D stimuli en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by