Random draw without re-delivery in a loop
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Pamela Pindi
el 28 de Oct. de 2020
Comentada: Pamela Pindi
el 4 de Nov. de 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 comentarios
Respuesta aceptada
Jan
el 28 de Oct. de 2020
Editada: Jan
el 28 de Oct. de 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
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!