Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slcount
Home > sltoolbox > utils > slcount.m

slcount

PURPOSE ^

SLCOUNT Count the number of sum entities

SYNOPSIS ^

function [nums, U] = slcount(A)

DESCRIPTION ^

SLCOUNT Count the number of sum entities

 $ Syntax $
   - nums = slcount(A)
   - [nums, U] = slcount(A)

 $ Arguments $
   - A:          the array (or cell array) containing things to be count
   - nums:       the resultant column vector of numbers
   - U:          the column vector or column cell array containing
                 the unique instances

 $ Description $
   - nums = slcount(A) counts the numbers of each entity contained in A.

   - [nums, U] = slcount(A) counts the numbers of each entity, and return
     the unique instances of these entities in order via U. 

 $ Remarks $
   # The instances belonging to the same type should be put together.
   # A can be a numeric array or a cell array. For numeric arrays, we
     use = for comparison, for cell array, we use isequal for comparison.

 $ Examples $
   - Count elements in a numeric array,
     \{
          A = [1 1 1 2 2 2 3 3 1 1 1 1];
          [n, U] = slcount(A)

          n = 
               3
               3
               2
               4

          U =
               1
               2
               3
               1
     \}

   - Count elements in a cell array
     \{
          A = {'a', 'a', 'a', 'b', 'b'};
          [n, U] = slcount(A)

          n =
              3
              2

          U =
              'a'
              'b'
     \}

 $ History $
   - Created by Dahua Lin on Nov 19th, 2005

CROSS-REFERENCE INFORMATION ^

This function calls:
This function is called by:

SOURCE CODE ^

0001 function [nums, U] = slcount(A)
0002 %SLCOUNT Count the number of sum entities
0003 %
0004 % $ Syntax $
0005 %   - nums = slcount(A)
0006 %   - [nums, U] = slcount(A)
0007 %
0008 % $ Arguments $
0009 %   - A:          the array (or cell array) containing things to be count
0010 %   - nums:       the resultant column vector of numbers
0011 %   - U:          the column vector or column cell array containing
0012 %                 the unique instances
0013 %
0014 % $ Description $
0015 %   - nums = slcount(A) counts the numbers of each entity contained in A.
0016 %
0017 %   - [nums, U] = slcount(A) counts the numbers of each entity, and return
0018 %     the unique instances of these entities in order via U.
0019 %
0020 % $ Remarks $
0021 %   # The instances belonging to the same type should be put together.
0022 %   # A can be a numeric array or a cell array. For numeric arrays, we
0023 %     use = for comparison, for cell array, we use isequal for comparison.
0024 %
0025 % $ Examples $
0026 %   - Count elements in a numeric array,
0027 %     \{
0028 %          A = [1 1 1 2 2 2 3 3 1 1 1 1];
0029 %          [n, U] = slcount(A)
0030 %
0031 %          n =
0032 %               3
0033 %               3
0034 %               2
0035 %               4
0036 %
0037 %          U =
0038 %               1
0039 %               2
0040 %               3
0041 %               1
0042 %     \}
0043 %
0044 %   - Count elements in a cell array
0045 %     \{
0046 %          A = {'a', 'a', 'a', 'b', 'b'};
0047 %          [n, U] = slcount(A)
0048 %
0049 %          n =
0050 %              3
0051 %              2
0052 %
0053 %          U =
0054 %              'a'
0055 %              'b'
0056 %     \}
0057 %
0058 % $ History $
0059 %   - Created by Dahua Lin on Nov 19th, 2005
0060 %
0061 
0062 if nargout >= 2
0063     bU = true;
0064 else
0065     bU = false;
0066 end
0067 
0068 if isnumeric(A) % processing numeric array (vectorized code)
0069     A = A(:);
0070     difs = diff(A);
0071     n = length(A);
0072     nums = [0; find(difs ~= 0); n];
0073     if bU
0074         U = A(nums(2:end));
0075     end
0076     nums = diff(nums);
0077     
0078 elseif iscell(A) % processing cell array (non-vectorizable code)
0079     A = A(:);
0080     n = length(A);
0081     difs = false(n, 1);
0082     for i = 1 : n-1
0083         if ~isequal(A{i}, A{i+1})
0084             difs(i) = 1;
0085         end
0086     end
0087     difs(n) = 1;
0088     nums = [0; find(difs)];
0089     if bU
0090         U = A(nums(2:end));
0091     end
0092     nums = diff(nums);
0093     
0094 else
0095     error('sltoolbox:invalidtype', ...
0096         'A should be a numeric array or a cell array');
0097 end
0098     
0099     
0100 
0101 
0102 
0103 
0104 
0105 
0106 
0107 
0108 
0109

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com