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

MatlabCountMatrix

Matlab Problem Overview


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?

Matlab Solutions


Solution 1 - Matlab

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)

Solution 2 - Matlab

Here's a list of all the ways I could think of to counting unique elements:

M = randi([1 7], [1500 1]);
Option 1: tabulate
t = tabulate(M);
counts1 = t(t(:,2)~=0, 2);
Option 2: hist/histc
counts2_1 = hist( M, numel(unique(M)) );
counts2_2 = histc( M, unique(M) );
Option 3: accumarray
counts3 = accumarray(M, ones(size(M)), [], @sum);
%# or simply: accumarray(M, 1);
Option 4: sort/diff
[MM idx] = unique( sort(M) );
counts4 = diff([0;idx]);
Option 5: arrayfun
counts5 = arrayfun( @(x)sum(M==x), unique(M) );
Option 6: bsxfun
counts6 = sum( bsxfun(@eq, M, unique(M)') )';
Option 7: sparse
counts7 = full(sparse(M,1,1));

Solution 3 - Matlab

One way you can perform this operation for all the values 1 through 7 at once is to use the function ACCUMARRAY:

>> M = randi(7,1500,1);  %# Some random sample data with the values 1 through 7
>> dayCounts = accumarray(M,1)  %# Will return a 7-by-1 vector

dayCounts =

   218       %# Number of Sundays
   200       %# Number of Mondays
   213       %# Number of Tuesdays
   220       %# Number of Wednesdays
   234       %# Number of Thursdays
   219       %# Number of Fridays
   196       %# Number of Saturdays

Solution 4 - Matlab

assume w contains week numbers ([1:7])

n = histc(M,w)

if you do not know the range of numbers in M:

n = histc(M,unique(M))

It is such as a SQL Group by command!

Solution 5 - Matlab

this would be perfect cause we are doing operation on matrix, and the answer should be a single number

sum(sum(matrix==value))

Solution 6 - Matlab

This is a very good function file available on Matlab Central File Exchange.

countmember.m link

This function file is totally vectorized and hence very quick. Plus, in comparison to the function being referred to in aioobe's answer, this function doesn't use the accumarray function, which is why this is even compatible with older versions of Matlab. Also, it works for cell arrays as well as numeric arrays.

SOLUTION : You can use this function in conjunction with the built in matlab function, "unique".

> occurance_count = countmember(unique(M),M)

occurance_count will be a numeric array with the same size as that of unique(M) and the different values of occurance_count array will correspond to the count of corresponding values (same index) in unique(M).

Solution 7 - Matlab

Use nnz instead of sum. No need for the double call to collapse matrices to vectors and it is likely faster than sum.

nnz(your_matrix == 5)

Doc

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNiko GamulinView Question on Stackoverflow
Solution 1 - MatlabaioobeView Answer on Stackoverflow
Solution 2 - MatlabAmroView Answer on Stackoverflow
Solution 3 - MatlabgnoviceView Answer on Stackoverflow
Solution 4 - MatlabBabakView Answer on Stackoverflow
Solution 5 - Matlabradhe_shyamView Answer on Stackoverflow
Solution 6 - MatlabAbhinavView Answer on Stackoverflow
Solution 7 - MatlabAndy CampbellView Answer on Stackoverflow