Create a while loop that fills a vector from 1-100

6 views (last 30 days)
I'm writing a collection consisting of 10 different items that's simulated through random numbers from 1-10 being assigned to the different items until I have at least one of each. After that, I need to write a code in order to simulate N=100 collections, save the total amount of items in each collection in a vector, and finally, analyze the average size of my collections and the size of my biggest collection. The collections themselves do not need to be saved.
This is how far i've come:
% These are the 10 different items
a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;
g = 0;
h = 0;
k = 0;
l = 0;
% This is the amount of collections
N = 100;
% This is meant to represent the loop for 100 collections
for n = 1:N
% The while loop gives the items a random number of 1-10 as long as at least one of them is below 1
while a < 1 || b < 1 || c < 1 || d < 1 || e < 1 || f < 1 || g < 1 || h < 1 || k < 1 || l < 1
a = a + randi(10);
b = b + randi(10);
c = c + randi(10);
d = d + randi(10);
e = e + randi(10);
f = f + randi(10);
g = g + randi(10);
h = h + randi(10);
k = k + randi(10);
l = l + randi(10);
if all(a & b & c & d & e & f & g & h & k & l)
% The loop ends when all of the items are above 0
end
end
end
I'm unsure how i should proceed from here in order to write a function that saves a value to 100 collections.

Answers (1)

Walter Roberson
Walter Roberson on 26 Feb 2021
Edited: Walter Roberson on 26 Feb 2021
Your design is wrong.
Each iteration, you should be creating a random integer 1 to the number of collections. Each time a particular value is generated, increment the count of the corresponding collection -- so if the random number was 17 then increment the count of the 17th collection.
Keep iterating until every collection has at least one item in it -- the count is at least one for each of the items.
After that you need to take the mean() of all of the counts over all of the collections, and you need to find the max() of the counts.
The code can be written in less than 10 lines.
  2 Comments
Eric Junaeus
Eric Junaeus on 26 Feb 2021
Thanks for the tip!
I do not know how i should create a random integer that represents the number of collections or how I should increment the count of the collections. Can you provide an example?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!