Entering a number in an array using MATLAB

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

+5
source share
3 answers

Use 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);
+10
source

Another method uses a function histif you are dealing with integers.

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.

+9

@Jacob is right: mode(v)will give you the answer you need.

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.

+1
source

All Articles