2 dice simulation. Code for when both dice = 6?

11 ビュー (過去 30 日間)
Aaron Brotherton
Aaron Brotherton 2014 年 11 月 4 日
コメント済み: Yusuf Ahmed 2022 年 6 月 22 日
*My first question wasn't clear.
I want to know how many double 6's there are basically.
For example,
a 6 2 6 2 6 1
b 6 4 5 6 6 3
I want something that would return the asnwer 2, indicating 2 double 6's, at positions 1 and 5.
Thanks*
Why won't this code tell me when a and b both equal 6?? I mean I know why, I just don't know what the right code is or how to correct it.
a=randi([1 6],n,1); disp('a='); disp(a);
b=randi([1 6],n,1); disp('b='); disp(b);
c=(a==6);d=(b==6); disp('c'); disp(c); disp('d'); disp(d);
e=(c==1);f=(d==1); disp('e=f'); disp(e==f);
The output I get is all of the times that (a==6)==(b==6) which I don't want.
NB: I've got an input code for n, don't worry.
Basically I need code that tells me the number of times both dice, a and b equal 6 at the same time/roll.
Thanks for any help.

採用された回答

James Tursa
James Tursa 2014 年 11 月 4 日
sum((a==6)&(b==6))
Looks like the same question as this newsgroup thread:
  1 件のコメント
Yusuf Ahmed
Yusuf Ahmed 2022 年 6 月 22 日
Oops! This page does not exist.

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

その他の回答 (3 件)

Orion
Orion 2014 年 11 月 4 日
編集済み: Orion 2014 年 11 月 4 日
ok, so
a = [6 2 6 2 6 1];
b = [6 4 5 6 6 3];
posInCommon = intersect(find(a==6),find(b==6))
posInCommon =
1 5
NUmberofoccurence = length(posInCommon)
NUmberofoccurence =
2

Image Analyst
Image Analyst 2014 年 11 月 4 日
編集済み: Image Analyst 2014 年 11 月 4 日
For a really simple solution, just check when the sum is 12:
numberOfDouble6s = sum((a+b)==12)
indexes = find((a+b)==12)

Orion
Orion 2014 年 11 月 4 日
I'm not sure to get what you're trying to do.
but a and b are vectors of size nx1.
If you want to know if a and b are the exacts same vectors :
isequal(a,b).
If you want to know if there is a 6 inside a, and inside b :
IsTherea6 = (any(a==6) && any(b==6))
  2 件のコメント
Orion
Orion 2014 年 11 月 4 日
and to get the number of 6 in a and in b
NumberOf6inA = nnz(a==6);
NumberOf6inB = nnz(b==6);
Aaron Brotherton
Aaron Brotherton 2014 年 11 月 4 日
編集済み: Aaron Brotherton 2014 年 11 月 4 日
Thanks for getting back so quick.
I want to know how many double 6's there are basically.
For example,
a 6 2 6 2 6 1
b 6 4 5 6 6 3
I want something that would return the asnwer 2, indicating 2 double 6's, at positions 1 and 5.
Thanks

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

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by