Creating a counting vector for "wins"

2 ビュー (過去 30 日間)
Elliot Gegen
Elliot Gegen 2019 年 2 月 27 日
コメント済み: Elliot Gegen 2019 年 3 月 10 日
Hello, I am doing an assignment for class which is called "even and odds" . It is similar to rock paper scissors but easier. What I am having trouble on is count the wins for each player. Here is my code
totalgames = 100;
odd_wins = zeros(1,totalgames);
even_wins = zeros(1,totalgames);
Totalgames = [1:totalgames];
for i=1:totalgames
x1(i) = randi(1:2,1);
x2(i) = randi(1:2,1);
total = x1(i)+x2(i);
if (total == 3);
odd_wins(i) = odd_wins(i) + 1;
else
even_wins(i) = even_wins(i) + 1;
end
end
My goal is to have odd_wins and even_wins row vector to keep track of how many times they have one for example [1,1,1,2,3,4,4,4,5...etc]. But these vectors always return [0,0,0,1,0,1,1...etc] where 0 means it lost and 1 means it won. How can I make it so it adds one to the value before it? Thank you!

採用された回答

Sreelakshmi S.B
Sreelakshmi S.B 2019 年 3 月 4 日
A simple solution:
totalgames = 100;
odd_wins = zeros(1,totalgames);
even_wins = zeros(1,totalgames);
%% special case for the first game
x1(1) = randi(2);
x2(1) = randi(2);
total = x1(1)+x2(1);
if (total == 3)
odd_wins(1) = 1;
else
even_wins(1) = 1;
end
for i=2:totalgames
x1(i) = randi(2);
x2(i) = randi(2);
total = x1(i)+x2(i);
if (total == 3)
odd_wins(i) = odd_wins(i-1) +1;
even_wins(i) = even_wins(i-1);
else
even_wins(i) = even_wins(i-1) + 1;
odd_wins(i) = odd_wins(i-1);
end
end
  1 件のコメント
Elliot Gegen
Elliot Gegen 2019 年 3 月 10 日
Thank you very much!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBiological and Health Sciences についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by