# I need help with a loop for randomly selecting from a growing pool

1 view (last 30 days)
Stephen Huckins on 17 Oct 2021
Answered: Pranjal Kaura on 26 Nov 2021
I have an experiment with 30 subjects, where we will be collecting two videos each day for each subject (for 21 days), and will need to randomly select these videos for review over 28 days from this growing pool. I will have to edit some videos, and I need a way to randomly select the videos so that they are not viewed on consecutive days, and not to be viewed more than 3 times. This is so that videos are not all reviewed early on in the experiment, or skewed towards the beginning.
In other words:
Day 1 - Record 2 events
Day 2 - Record 2 events...
Day 21 - Record two events
The review of the videos is as follows:
Day 2 - Review 1 videos
Day 3- Review 2 videos
Day 4- Review 3 videos
Day 5- Review 4 videos
Days 6- Review 5 videos...
Day 28- Review 5 videos
To reiterate, there will be 42 videos to be recorded, and I cant have the videos reviewed on consecutive days, or more than 3 or 4 times.
This is the code that I have created thus far, but I am having trouble with the videos being not selected. For example, when I added code to see when is the first time and the last review of a particular video, I noticed that some of the videos were not being selected. I need every video to be selected.
This is the code I have:
output = cell(25, 28);
% separate video pool for each participant
% 2 new videos added every day
% 1-5 videos to be review each day
% any one video can only be reviewed at most (4?) times
% same video cannot be reviewed on consecutive days
for participant = 1:30
videoPool = [1, 2];
videoUsed = zeros(42, 1);
prevVideos = []; % videos from previous day to ignore
maxedVideos = []; % videos viewed (4?) times to ignore
dayRanges = zeros(28,2);
for day = 2:28
% determine how many videos we're selecting
if day < 5
numVideos = day - 1;
else
numVideos = 4;
end
% randomly select videos from videoPool
validVideos = setdiff(videoPool, prevVideos);
validVideos = setdiff(validVideos, maxedVideos);
videos = randsample(validVideos, numVideos);
% save list of videos in output matrix
output{participant, day} = videos;
% keep track of videos for next day
prevVideos = videos;
% add counts to videoUsed list
for vid = videos
videoUsed(vid) = videoUsed(vid) + 1;
if videoUsed(vid) == 1
dayRanges(vid,1) = day;
end
if videoUsed(vid) == 3
dayRanges(vid,2) = day;
maxedVideos = [maxedVideos, vid];
end
end
if day <= 21
videoPool = [videoPool, (day*2-1:day*2)];
end
end
disp(dayRanges);
end

Pranjal Kaura on 26 Nov 2021
Hey Stephen,
I found the following problem in the logic of your code.
Per participant:
Total number of videos that you want to review = (Total number of videos recorded) x (Max Review per Video). Herein this is equal to 42x3 = 126.
Total number of slots that you have to review these videos = 1(Day1) + 2(Day2) + 3 + 4 + 4 +... 4(Day28) = 6 + 4x24 = 102.
Since number of slots (for reviews) are less than the total number of videos to review, it's possible that some of the videos might not be selected in a particular model run. This is because you're also adding randomness in the model when you use the ' randsample(validVideos, numVideos)' command.
To make sure that all the videos are selected, you need to keep the slots/buckets available for review to be greater than the the total number of videos to review.
Hope this helps!

R2021b

### Community Treasure Hunt

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

Start Hunting!