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

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)

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/495341-i-want-the-code-to-repeat-for-10-000-times-and-add-the-new-value-to-the-matrix-but-i-don-t-know-how#comment_775476

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/495341-i-want-the-code-to-repeat-for-10-000-times-and-add-the-new-value-to-the-matrix-but-i-don-t-know-how#comment_775476

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/495341-i-want-the-code-to-repeat-for-10-000-times-and-add-the-new-value-to-the-matrix-but-i-don-t-know-how#comment_775487

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/495341-i-want-the-code-to-repeat-for-10-000-times-and-add-the-new-value-to-the-matrix-but-i-don-t-know-how#comment_775487

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/495341-i-want-the-code-to-repeat-for-10-000-times-and-add-the-new-value-to-the-matrix-but-i-don-t-know-how#comment_775489

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/495341-i-want-the-code-to-repeat-for-10-000-times-and-add-the-new-value-to-the-matrix-but-i-don-t-know-how#comment_775489

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/495341-i-want-the-code-to-repeat-for-10-000-times-and-add-the-new-value-to-the-matrix-but-i-don-t-know-how#comment_775490

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/495341-i-want-the-code-to-repeat-for-10-000-times-and-add-the-new-value-to-the-matrix-but-i-don-t-know-how#comment_775490

Sign in to comment.