Sampling images without replacement

4 Ansichten (letzte 30 Tage)
Pamela Pindi
Pamela Pindi am 5 Nov. 2020
Beantwortet: Dave B am 5 Nov. 2020
Hi everyone,
I have 10 trials and 2 conditions (neutral and condition). I have to random draw 10 images from a folder of 100 images without replacement for each trial and for each condition and to display it with psychtoolbox. I created a function, but images are sometimes repeating.
function print_random_images(w, type, deviceIndex, path, img,folder, N, text, ...
backgroundColor, textColor, dispW, dispH, slack)
images = dir(fullfile(path, '*.jpg'));
files = fullfile({images.folder}, {images.name});
index = 1:numel(files);
if type == 0 % neutral condition
displayTextCentered(w, text, backgroundColor,textColor, 1.5,50, 'center', 'center');
waitTTL(deviceIndex)
for i = 1:numel(files) / 10
selected = randperm(numel(index), N); % N = 10
file = img{selected};
image = imread(fullfile(folder,file));
imageDisplay = Screen('MakeTexture',w,image);
showImagesAndFixationCross(w,backgroundColor,dispW,dispH,slack, imageDisplay)
index(selected) = [];
end
elseif type == 1 % regulation condition
displayTextCentered(w, text, backgroundColor,textColor, 1.5,50, 'center', 'center');
waitForSpaceKeyPress()
for i= 1:numel(files)/ 10
selected = randperm(numel(index), N); % N = 10
file = img{selected};
image = imread(fullfile(folder, file));
imageDisplay = Screen('MakeTexture', w, image);
showImagesAndFixationCross(w,backgroundColor,dispW,dispH,slack, imageDisplay)
end
end
end
Can someone help me, please ??
Thanks a lot

Antworten (1)

Dave B
Dave B am 5 Nov. 2020
Hi Pamela:
It looks like you're calling randperm for each iteration of your loop, but I think you just want to call randperm once right? I'm not sure I followed your code exactly, partly because I was confused about img (the input argument) vs. images (where you have the file name).
Here's some code that shows a generic loop which operates on 10 unique (no replacement) random files:
all_filenames=... % Here's where you get a list of all filenames, one per cell, presumably 100 long
ind = randperm(100,10); % list of 10 unique values selected from 1:100
for i = 1:10
this_filename = all_filenames{ind(i)}; % get the filename from one of the randomly selected values
% here's where you'd do something with the filename
end

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by