Random draw without re-delivery in a loop

조회 수: 3 (최근 30일)
Pamela Pindi
Pamela Pindi 2020년 10월 28일
댓글: Pamela Pindi 2020년 11월 4일
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
  댓글 수: 1
Pamela Pindi
Pamela Pindi 2020년 11월 4일
Thanks for your answer !
My input data are a list of 60 images into a folder

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by