Showing posts with label frequency. Show all posts
Showing posts with label frequency. Show all posts

Friday, June 21, 2013

Count the frequency of elements in a vector or matrix- part 3

Option 5: arrayfun
counts5 = arrayfun( @(x)sum(M==x), unique(M) );
A = arrayfun(fun, S) applies the function specified by fun to each element of array S, and returns the results in array A.” This approach is quite straightforward. The following is an example.
M = [1; 2; 6; 2; 6]; 
counts5 = arrayfun( @(x)sum(M==x), unique(M) )

counts5 =

     1
     2
     2

Option 6: bsxfun
counts6 = sum( bsxfun(@eq, M, unique(M)') )';
The function “bsxfun” applies element-by-element binary operation to two arrays. Though this method is not hard, but it is easier to understand if we do it step by step.
M = [1; 2; 6; 2; 6];
unique(M)'

ans =

     1     2     6

bsxfun(@eq, M, unique(M)')

ans =

     1     0     0
     0     1     0
     0     0     1
     0     1     0
     0     0     1


counts6 = sum( bsxfun(@eq, M, unique(M)') )'

counts6 =

     1
     2
     2

Option 7: sparse
counts7 = full(sparse(M,1,1));


Again, for this approach, we need to check the MATLAB help file first. “S = sparse(i,j,s,m,n,nzmax) uses vectors i, j, and s to generate an m-by-n sparse matrix such that S(i(k),j(k)) = s(k), with space allocated for nzmax nonzeros. Vectors i, j, and s are all the same length. Any elements of s that are zero are ignored, along with the corresponding values of i and j. Any elements of s that have duplicate values of i and j are added together.” An example is given below.

sparse(M,1,1)

ans =

   (1,1)        1
   (2,1)        2
   (6,1)        2

counts7 = full(sparse(M,1,1))

counts7 =

     1
     2
     0
     0
     0
     2

Part 1: http://nw360.blogspot.com.au/2013/06/count-number-of-elements-in-vector-or.html
Part 2: http://nw360.blogspot.com.au/2013/06/count-number-of-elements-in-vector-or_20.html
Part 3: http://nw360.blogspot.com.au/2013/06/count-number-of-elements-in-vector-or_21.html

References: 
1 MATLAB help file.
2 http://stackoverflow.com/questions/2880933/how-can-i-count-the-number-of-elements-of-a-given-value-in-a-matrix

Thursday, June 20, 2013

Count the frequency of elements in a vector or matrix- part 2

Option 3: accumarray
counts3 = accumarray(M, ones(size(M)), [], @sum);
% or simply: accumarray(M, 1);

This approach is not easy to understand. Let’s see what the function accumarry does first. MATLAB help file states that “accumarray groups elements from a data set and applies a function to each group. A = accumarray(subs,val) creates an array A by accumulating elements of the vector val using the elements of subs as indices. The position of an element in subs determines which value of vals it selects for the accumulated vector; the value of an element in subs determines the position of the accumulated vector in the output”.
So what does it mean exactly? Let’s see an example.
subs = [1; 2; 4; 2; 4];
val = ones(1,5);
A = accumarray(subs, val)

A =

     1 % A(1) = val(1) = 101
     2 % A(2) = val(2)+val(4) = 1+1 = 2
     0 % A(3) = 0
     2 % A(4) = val(3)+val(5) = 1+1 = 2
Thus, when we set the input argument “val” as an array of all ones, the function accumarray actually counts the frequency of each element in the first input.
Option 4: sort/diff

[MM idx] = unique( sort(M) );
counts4 = diff([0;idx]);
This approach first finds out the unique element in the input vector, then use “diff” to find out the frequency of each elements. Again, an example is a good way to learn.
M = [1; 2; 4; 2; 4; 5; 2]; % input is a column vector
[MM idx] = unique( sort(M) )
MM =

     1
     2
     4
     5


idx =

     1
     4
     6
     7

counts4 = diff([0;idx])

counts4 =

     1
     3
     2
     1
In the first step of the approach, “unique” is applied on the sorted input. The returned indexes of the unique elements contains the information of the frequency. Then “diff” is used to extract the frequency information. Note that the input is a column vector. If it is a row vector, in the last step, it should be
counts4 = diff([0 idx])
 


Part 1: http://nw360.blogspot.com.au/2013/06/count-number-of-elements-in-vector-or.html
Part 2: http://nw360.blogspot.com.au/2013/06/count-number-of-elements-in-vector-or_20.html
Part 3: http://nw360.blogspot.com.au/2013/06/count-number-of-elements-in-vector-or_21.html

References: 
1 MATLAB help file.
2 http://stackoverflow.com/questions/2880933/how-can-i-count-the-number-of-elements-of-a-given-value-in-a-matrix

Tuesday, June 04, 2013

Count the frequency of elements in a vector or matrix- part 1

In MATLAB, it is a common task that find out how many time an element that appears in a vector or matrix. If the element is known, the intuitive idea that comes up in my mind is to use the following code
M = randi([1 5], [5 3]); % M is the matrix
sum(sum(M ==2)); % 2 is the element to be counted

However, sometimes we want to count all the elements in a matrix or a vector, what should we do? Amro has given several approaches to count the elements without detailed explanation. I think the best way to learn is to understand why we use the code. Thus in the following, explanations are given for the approaches by Amro.

Option 1: tabulate

t = tabulate(M); % frequency count for a vector
counts1 = t(t(:,2)~=0, 2); % find out the frequency of all the elements existing in the vector


TABLE = tabulate(x) creates a frequency table of data in vector x. Information in TABLE is arranged as follows:
  • 1st column — The unique values of x
  • 2nd column — The number of instances of each value
  • 3rd column — The percentage of each value
However, this approach requires MATLAB Statistics Toolbox and the input must be a vector.
 

Option 2: hist/histc

counts2_1 = hist( M, numel(unique(M)) );
counts2_2 = histc( M, unique(M) );
% unique: find unique elements of vector
% numel: mumber of elements in array

    n = hist(Y,x) where x is a vector, returns the distribution of Y among length(x) bins with centers specified by x.

    n = histc(x,edges) counts the number of values in vector x that fall between the elements in the edges vector (which must contain monotonically nondecreasing values).

Note the difference between the use of hist and histc.

Part 1: http://nw360.blogspot.com.au/2013/06/count-number-of-elements-in-vector-or.html
Part 2: http://nw360.blogspot.com.au/2013/06/count-number-of-elements-in-vector-or_20.html
Part 3: http://nw360.blogspot.com.au/2013/06/count-number-of-elements-in-vector-or_21.html

References:
1 MATLAB help file.
2 http://stackoverflow.com/questions/2880933/how-can-i-count-the-number-of-elements-of-a-given-value-in-a-matrix