A group of 12 people all roll a pair of dice 12 times, how often does each person roll a total number that is greater than or equal to 7?

2 ビュー (過去 30 日間)
Find: - Create variable roll1, which is full of random integers in the interval [1,6] and has 12 rows (1 for each person's roll) and 12 columns (1 for each person participating).
- Create variable roll2, which is also an array of random integers in the interval [1,6] with the same size as roll1.
- Create variable total that contains total of each roll of both dice (this is very similar to one of your lab exercises).
- Create variable GT7, which is a logical array that contains a true or false depending on whether each element is greater than or equal to 7.
- How many times does each person roll a total number greater than or equal to 7? Remember that each column represents a different person's roll. Save this vector as totalGT7.
- Which person (numbered 1 to 12 by element number) rolled a value greater than or equal to 7 the most? Save this as most. (remember your find command and your max command)
- Which person had the least number of rolls that totaled 7 or greater? Save this as least.
...
Just want to double check my code here, even though I know there are errors; specifically totalGT7 and most.
roll1=randi(6,12);
roll2=randi(6,12);
total=roll1+roll2;
GT7=total>7;
totalGT7=GT7>=1;
most=max(totalGT7);
least=find(totalGT7<7);

採用された回答

Torsten
Torsten 2024 年 2 月 20 日
編集済み: Torsten 2024 年 2 月 20 日
In my opinion, it should be
roll1=randi(6,12);
roll2=randi(6,12);
total=roll1+roll2;
GT7 = total>=7;
totalGT7 = sum(GT7,1);
most = find(totalGT7==max(totalGT7));
least = find(totalGT7==min(totalGT7));
  1 件のコメント
Spaceman
Spaceman 2024 年 2 月 20 日
Eureka! This is 100% correct. I don't know why I can't wrap my brain around logcal indexing quite yet... Thank you.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2024 年 2 月 20 日
Your first four lines of code are correct.
- How many times does each person roll a total number greater than or equal to 7? Remember that each column represents a different person's roll. Save this vector as totalGT7.
Your code for this is not correct. Let's say I gave you a smaller example.
A = [1 0 0; 1 0 1; 1 0 0]
A = 3×3
1 0 0 1 0 1 1 0 0
Describe how you would compute totalGT7 from this A matrix by hand. Then determine how you can compute that using MATLAB code. I'll give you a hint: one of the Basic Arithmetic functions on this documentation page will be useful.
- Which person (numbered 1 to 12 by element number) rolled a value greater than or equal to 7 the most? Save this as most. (remember your find command and your max command)
Your code for this doesn't answer this particular question. It can tell you the greatest number of times a person rolled 7 or more, but not which person achieved that feat. You can do this with find and max but I'd skip the find call. Take a look at the max documentation page for a syntax that will tell you both the greatest number of 7 or more rolls and whose rolls those were.
- Which person had the least number of rolls that totaled 7 or greater? Save this as least.
Your code doesn't answer this. You know the max function; do you know (or can you guess and check by looking at its documentation page) what the function for finding the opposite of the max-imum would be?
  1 件のコメント
Spaceman
Spaceman 2024 年 2 月 20 日
Thank you very much for these explanations. I appreciate you. Sometimes I just need to see the code to understand how to apply it to the question at hand. To me it's not always so intuitive.

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

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by