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.

조회 수: 5 (최근 30일)
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
Image Analyst
Image Analyst 2019년 12월 7일
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
Victor Popa-Simil 2019년 12월 7일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 12월 7일
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)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by