Possible duplicate:Determining the number of occurrences of each unique element in a vector
I have the following array:
v = [ 1 5 1 6 7 1 5 5 1 1]
And I need to count the values ββand show a number that looks more.In the example above, the solution will be 1 (there are five 1)
Thanks in advance
Use mode.
mode
If you also need to return the number of elements, do the following:
m = mode(v); n = sum(v==m); fprintf('%d appears %d times\n',m,n);
Another method uses a function histif you are dealing with integers.
hist
numbers=unique(v); %#provides sorted unique list of elements count=hist(v,numbers); %#provides a count of each element occurrence
Just make sure you set the output value for the hist function, or you get a histogram.
@Jacob is right: mode(v)will give you the answer you need.
mode(v)
I just wanted to add a nice way to represent the frequencies of each value:
bar(accumarray(v', 1))
will display a character chart counting each value in v.
v