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?
1 次查看(过去 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);
0 个评论
采纳的回答
更多回答(1 个)
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]
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!