Created
February 10, 2022 22:53
-
-
Save kleinerm/084de715c21c1154258a800484522a51 to your computer and use it in GitHub Desktop.
Democode for https://psychtoolbox.discourse.group/t/request-for-priority-support-psychtoolbox-crashing-unpredictably/4241
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trial_number = 90; | |
% Input set of images: | |
test_images = [1,2,3,4,5] | |
nrImages = length(test_images); | |
% Present each 3 times: | |
max_image_presentations = trial_number / nrImages | |
tic | |
if 1 | |
% More efficient procedure: | |
% Provide initial set of test_indices, randomized, nrImages repetitions for | |
% each image index: | |
test_indices = BalanceFactors(max_image_presentations, 1, 1:nrImages); | |
test_index = []; | |
test_index_sequence = nan(1, trial_number); | |
for i=1:trial_number | |
% Try to find an index out of the remaining indices that is not the same as | |
% in last iteration: | |
while (test_index == test_indices(1)) && length(unique(test_indices)) > 1 | |
% Reshuffle if no success: | |
test_indices = Shuffle(test_indices); | |
end | |
% Got one, take it and take it out of set of remaining test_indices for | |
% next trial: | |
test_index = test_indices(1); | |
test_indices = test_indices(2:end); | |
test_index_sequence(i) = test_index; | |
end | |
% Done. test_index_sequence contains whole sequence of test_index values: | |
test_index_sequence | |
else | |
% Less efficient procedure, prone to hangs or infinite loops: | |
tracker = zeros(1,length(test_images)); | |
for i=1:trial_number | |
image_chosen = false; | |
while image_chosen == false | |
test_index = randi(length(test_images)); | |
if i == 1 | |
image_chosen = true; | |
elseif test_index ~= previous_test_index &&... | |
tracker(test_index) < max_image_presentations | |
image_chosen = true; | |
end | |
end | |
cnt = i | |
previous_test_index = test_index | |
tracker(test_index) = tracker(test_index) + 1; | |
test_index_sequence(i) = test_index; | |
end | |
% Done. test_index_sequence contains whole sequence of test_index values: | |
test_index_sequence | |
end | |
toc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment