Random draw without re-delivery in a loop

1 次查看(过去 30 天)
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

采纳的回答

Jan
Jan 2020-10-28
编辑:Jan 2020-10-28
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

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by