MATLAB Answers

# I want the code to repeat for 10,000 times and add the new value to the matrix but I don't know how? Please help.

4 views (last 30 days)
This is my code so far and I added the "for k" and "end" in hopes of making the code loop but it didnt work. The goal of the project is to have values from 1-100 which start out with a value of 100 and then 2 are randomly selected from this group of 100. There is a random choosing of who wins and if they win, their value (100) gets a +1 applied to it. If they lose they get -1. I want 10,000 iterations where any number can be chosen.
nsims = 10000;
for k = 1:nsims
people = [1:100];
for i=1:100
people(i) = 100;
end
simsize = numel(people);
y = (randperm(simsize, 2));
if round(rand) == 0
output = y(2);
else
output = y(1);
end
win = people(y(1)) + 1;
lose = people(y(2)) - 1;
dist = [win; lose];
end

#### 4 Comments

Show 1 older comment
Victor Popa-Simil on 7 Dec 2019
The people are labeled as 1 to 100 and their value is 100. Two people are randomly selected at a time and one of them will win. Whoever wins will get +1 and whoever loses will get -1. I select the winner by using round(rand) to randomly select either a 0 or a 1 and I assign a person a 0 and a person a 1. Whoever gets the 1 wins and they get +1. I want this to happen 10,000 times.
Image Analyst on 7 Dec 2019
Sorry, I just don't know what it means when you say that the value of each person is 100. How does a person have a value of 100?
I understand that each person will have counts:
1. a count for how many times they won (which you call wins), and
2. a count of how many times they lost (which you call lose),
but what is this other value of 100? Are you initializing one of those counts to 100 in advance for some reason???
So, so far I know that you will have a vector "counts" and the index of counts is the person number, so if the counts is the number of wins, counts(1) is the number of wins person #1 has, counts(2) is the number of wins person #2 has, counts(3) is the number of wins person #3 has, etc.
So, for each of the 10,000 trials (experiments) will you be taking 2 people (one pair) at random, or 100 pairs of people at random from the group of 100 people?
When a person wins (or loses) does the other person lose (or win) - the opposite of the first person? Or is each person's win or loss totally unrelated to whether the other person won or lost?
Victor Popa-Simil on 7 Dec 2019
The person has an inherent value of 100 or say 100 objects for example. And when they interact with another randonmly selected individual one will win an object (+1) and another will lose that object (-1). One iteration is a random selection of two individuals who interact. The second iteration will be another random selection of two individuals and it can be the same individuals as selected in the first as well. I was confused on how to show this process. I was able to achieve the first iteration but I couldn't complete the rest of the 10,000.

Sign in to comment.

### Accepted Answer

JESUS DAVID ARIZA ROYETH on 7 Dec 2019
nsims = 10000;
people = repmat(100,1,100);
simsize = numel(people);
for k = 1:nsims
y = randperm(simsize, 2);
if round(rand) == 0
win=y(1);
lose=y(2);
else
win=y(2);
lose=y(1);
end
people(win)=people(win)+1;
people(lose)=people(lose)-1;
end
disp(people)

#### 1 Comment

Victor Popa-Simil on 7 Dec 2019
Thank you!

Sign in to comment.

### More Answers (0)

Sign in to answer this question.