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


2 comments:

  1. Hello Friends,

    I have a m x n matrix. I want to compute the probability of each element occurring in a column. I want to do it for each column.

    For instance, suppose we have a 3 x 4 matrix:

    A = [1 1 3 2; 2 3 0 2; 3 1 0 2];

    Then, I would like to have the following matrix of probabilities: _P(A) = [ 1/3 2/3 1/3 1; 1/3 1/3 2/3 1; 1/3 2/3 2/3 1];_

    Here, entries in P(A) are the probabilities of each element within the column.

    For the 1st column: p(1) = p(2) = p(3) = 1/3

    For the 2nd column: p(1) = 2/3, p(3) = 1/3

    For the 3rd column: p(3) = 1/3, p(0) = 2/3

    For the 4th column: p(2) = 3/3 = 1.

    Please advise.

    ReplyDelete
    Replies
    1. Hi, simply treat one column as a vector and count the frequency of each elements, the divided by the number of the element in the column.

      Delete