how to find repeated values in array?
显示 更早的评论
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
2021-3-23
0 个投票
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!