Sampling images without replacement
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Dave B
2020년 11월 5일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Image display and manipulation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!