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.

Risposte (1)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Richiesto:

il 23 Mar 2021

Risposto:

il 23 Mar 2021

Community Treasure Hunt

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

Start Hunting!

Translated by