Random draw without re-delivery in a loop

2 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by