PTB: display sampling images without replacement

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. 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*
    
  •            displayTextCentered(w, text, backgroundColor,textColor, 1.5,50, 'center', 'center');*
    
  •            waitTTL(deviceIndex)*
    
  •            *
    
  •            for i = 1:numel(files) / 2*
    
  •                selected = randperm(numel(index),N);*
    
  •                file = img{selected};*
    
  •                image = imread(fullfile(folder,file));*
    
  •                imageDisplay = Screen('MakeTexture',w,image);*
    
  •                showImagesAndFixationCross(w,backgroundColor,dispW,dispH,slack, imageDisplay)*
    
  •            end*
    
  •        elseif type == 1*
    
  •            displayTextCentered(w, text, backgroundColor,textColor, 1.5,50, 'center', 'center');*
    
  •            waitForSpaceKeyPress()*
    
  •            *
    
  •            for i= 1:numel(files)/ 2*
    
  •                selected = randsample(numel(index),N);*
    
  •                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

I suspect you just want to move the line selected = randperm(numel(index),N); outside your for loop. The line itself is fine, it selects indices for you without replacement, so randperm(100, 10) gives you 10 indices from 1:100 without replacement.

Btw, this is not a Psychtoolbox-specific question. For general programming questions you probably get mor / faster / better responses at stackoverflow, mathworks, etc.