How can I count the number of elements of a given value in a matrix?

Niko Gamulin picture Niko Gamulin · May 21, 2010 · Viewed 174.2k times · Source

Does anyone know how to count the number of times a value appears in a matrix?

For example, if I have a 1500 x 1 matrix M (vector) which stores the values of weekdays (1 - 7), how could I count how many Sundays (1), Mondays(2), ... , Saturdays(7) are stored in M?

Answer

aioobe picture aioobe · May 21, 2010

Have a look at Determine and count unique values of an array.

Or, to count the number of occurrences of 5, simply do

sum(your_matrix == 5)