Random draw without re-delivery in a loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Pamela Pindi
am 28 Okt. 2020
Kommentiert: Pamela Pindi
am 4 Nov. 2020
Hi all,
I have to create a loop in which I have to pick 5 images without resetting at each turn.
For the randomization I did this :
randomizedTrials_neutral = randperm(nTrials_neutral_run,5)
But I don't know how to insert it in a loop to randomly pick out 5 images without resetting at each turn.
Would you know how I can do this, please?
Thanks for your help
0 Kommentare
Akzeptierte Antwort
Jan
am 28 Okt. 2020
Bearbeitet: Jan
am 28 Okt. 2020
What are your input data? A list of files stored in a folder? (As usual: please explain this instead of letting the readers guess the details.) Then:
List = dir(fullfile('C:\Your\Folder', '*.jpg'));
File = fullfile({List.folder}, {List.Name});
Index = 1:numel(File);
for k = 1:numel(File) / 5
selected = randperm(numel(Index), 5); % Select 5 of the existing images
fprintf('Round %d:\n', k); % Show them (or do whatever you need)
fprintf(' %s\n', File(Index(selected)));
Index(selected) = []; % Remove selected images from the list
end
Another simple approach:
List = dir(fullfile('C:\Your\Folder', '*.jpg'));
File = fullfile({List.folder}, {List.Name});
Index = randperm(numel(File));
Blocks = reshape(Index, 5, []); % Assuming than #files is multiple of 5
% Now the column vector Blocks(:, k) contains the indices of 5 randomly selected images.
for k = 1:size(Blocks, 2)
fprintf(' %d', Blocks(:, k));
fprintf('\n');
end
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!