フィルターのクリア

How to handle cells?

1 回表示 (過去 30 日間)
Dirk te Brake
Dirk te Brake 2023 年 9 月 19 日
コメント済み: Dirk te Brake 2023 年 9 月 20 日
I'm making a Rock paper scissors game but have problems using cell variabels. I have a 9x2 cell variable i want to find on which row both colums have a 1, how do i do this? In this case I would like to know the position of row 2 so i can display the right outcome form the result array.
r = 'r'; s = 's'; p = 'p'; d = "Its a Draw!"; u1 = "User1 wins!"; u2 = "User2 wins!";
results = [r, r, d;
r, s, u1;
r, p, u2;
s, s, d;
s, p, u1;
s, r, u2;
p, p, d;
p, r, u1;
p, s, u2];
Res1 = strfind(results(:,1),'r');
Res2 = strfind(results(:,2),'s');
Res = [Res1, Res2];
disp(Res)
{[ 1]} {0×0 double} {[ 1]} {[ 1]} {[ 1]} {0×0 double} {0×0 double} {[ 1]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 1]}

採用された回答

Matt J
Matt J 2023 年 9 月 20 日
編集済み: Matt J 2023 年 9 月 20 日
I would use logical arrays instead of cells.
r = 'r'; s = 's'; p = 'p'; d = "Its a Draw!"; u1 = "User1 wins!"; u2 = "User2 wins!";
results = [r, r, d;
r, s, u1;
r, p, u2;
s, s, d;
s, p, u1;
s, r, u2;
p, p, d;
p, r, u1;
p, s, u2];
Lookup = results(:,1:2)==["r","s"]
Lookup = 9×2 logical array
1 0 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 1
row=find(all(Lookup,2))
row = 2
outcome=results(row,3)
outcome = "User1 wins!"
  1 件のコメント
Dirk te Brake
Dirk te Brake 2023 年 9 月 20 日
I was looking for exactly that, thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by