If we were rolling four 6-sided dice and our array shows that we rolled 2 3 3 2 then the function would count and return that 2 values were repeated (a pair of 2's and a pair of 3's). If we were rolling four 6-sided dice and our array shows that we rolled 2 2 2 2 then the function would count and return that 1 value was repeated (the 2's). Note that in this final example we don't count 2's more than once.
countRepeats([2 3 3 2], 6)
countRepeats([2 2 2 2], 6)
function countRepeats(dieValue,numSides)
countRepeats = hist(dieValue,1:max(dieValue))
end
I tried using this function but it only counts the number of values of each number shown starting from 1. I think I might need a loop for this. Any hints are appriciated.

回答 (1 件)

Star Strider
Star Strider 2021 年 3 月 23 日

0 投票

See if the approach in this Answer will do what you want.

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

タグ

質問済み:

2021 年 3 月 23 日

回答済み:

2021 年 3 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by