Selecting n random elements within a for loop
1 回表示 (過去 30 日間)
古いコメントを表示
Hi everyone,
So assume I have a dataset with 10 folders (each folder corresponding to a category).
In my example :
ListOfAllCategories is a 1x10 cell containing the list of all categories
ImageListForEachCategory is a 10x1 map that returns all images belonging to a given category.
My goal is to (1) "extract" all images belonging to all 10 categories and (2) draw n random images from the whole pool (all categories combined). However, I am encountering problems at the very beginning of the code. I am doing the following (to begin with) :
for i=1:length(ListOfAllCategories)
A = ListOfAllCategories{i};
B = ImageListForEachCategory(A);
disp(B)
end
When I display "B", I get a list of all the images from all categories but the folder in the workspace contains only the images belonging to the first category. Should I use a second For Loop in that case? Also, is there a different way to directly draw n images from the whole pool?
Thank you very much.
Best regards.
0 件のコメント
採用された回答
Image Analyst
2020 年 9 月 14 日
Have you seen dir() with two **?
% Get a list of all PNG files in all subfolders of topLevelFolder.
allFiles = dir([topLevelFolder, '**/*.png']); % All files in all 10 subfolders.
% Get only a subset of n of them:
randomIndexes = randperm(length(allFiles), n);
subset = allFiles(randomIndexes)
7 件のコメント
Image Analyst
2020 年 9 月 19 日
Yes, you can call my attached function twice to get the grandparent of a folder. If it's the same as the top level folder, it's exactly 2 levels deep.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Multidimensional Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!