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

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 M matrix (vector) that stores weekday values โ€‹โ€‹(1-7), how can I calculate how many Sundays (1), Mondays (2), ..., Saturday (7 ) are stored in M ?

+63
matrix count matlab
May 21 '10 at 9:29 a.m.
source share
7 answers

See Define and Count Unique Array Values .

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

 sum(your_matrix == 5) 
+92
May 21 '10 at 9:31
source share

Here are all the ways in which you could count unique elements:

 M = randi([1 7], [1500 1]); 

Option 1: table

 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: Drive

 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)); 
+70
May 21 '10 at 19:59
source share

One way to perform this operation for all values โ€‹โ€‹1 through 7 is to use the ACCUMARRAY function:

 >> 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 
+9
May 21 '10 at 13:26
source share

Suppose 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)) 

He is like the SQL Group on command!

+5
Apr 15 2018-12-12T00:
source share

That would be the ideal reason we are doing the matrix operation, and the answer should be a single number

 sum(sum(matrix==value)) 
+4
Jun 27 '13 at 15:37
source share

This is a very good feature file, available on the central Matlab file server.

reference countmember.m

This function file is fully vectorized and therefore very fast. In addition, compared to the function that is mentioned in the answer on aioobe, this function does not use the accumulation function, therefore it is even compatible with older versions of Matlab. In addition, it works with both cell arrays and numeric arrays.

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

occurance_count = countmember (unique (M), M)

occurance_count will be a numerical array with the same size as the unique (M), and different values โ€‹โ€‹of the occurance_count array will correspond to the count of the corresponding values โ€‹โ€‹(of the same index) in the unique (M).

+3
Sep 29 '11 at 10:37
source share

Use nnz instead of the sum. There is no need for a double call to collapse matrices to vectors and, most likely, faster than the sum.

 nnz(your_matrix == 5) 

Doc

+2
Aug 31 '15 at 21:08
source share



All Articles