Home > utility > elementCount.m

elementCount

PURPOSE ^

elementCount: Count elements in a vector.

SYNOPSIS ^

function [sortedElement, count] = elementCount(in)

DESCRIPTION ^

elementCount: Count elements in a vector.
    Usage: [sortedElement, count] = elementCount(in)

    For example:
        in = [5 1 5 5 7 9 9];
        fprintf('in = %s\n', mat2str(in));
        fprintf('"[sortedElement, count] = elementCount(in)" produces the following output:\n');
        [sortedElement, count] = elementCount(in);
        fprintf('sortedElement = %s\n', mat2str(sortedElement));
        fprintf('count = %s\n\n', mat2str(count));
        in = {'ab', 'cd', 'ef', 'ab', 'cd', 'ab'};
        fprintf('in = %s\n', '{''ab'', ''cd'', ''ef'', ''ab'', ''cd'', ''ab''}');
        fprintf('"[sortedElement, count] = elementCount(in)" produces the following output:\n');
        [sortedElement, count] = elementCount(in);
        fprintf('sortedElement = %s\n', '{''ab'', ''cd'', ''ef''}');
        fprintf('count = %s\n', '[3 2 1]');

    Type "elementCount" for a self demo.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [sortedElement, count] = elementCount(in)
0002 %elementCount: Count elements in a vector.
0003 %    Usage: [sortedElement, count] = elementCount(in)
0004 %
0005 %    For example:
0006 %        in = [5 1 5 5 7 9 9];
0007 %        fprintf('in = %s\n', mat2str(in));
0008 %        fprintf('"[sortedElement, count] = elementCount(in)" produces the following output:\n');
0009 %        [sortedElement, count] = elementCount(in);
0010 %        fprintf('sortedElement = %s\n', mat2str(sortedElement));
0011 %        fprintf('count = %s\n\n', mat2str(count));
0012 %        in = {'ab', 'cd', 'ef', 'ab', 'cd', 'ab'};
0013 %        fprintf('in = %s\n', '{''ab'', ''cd'', ''ef'', ''ab'', ''cd'', ''ab''}');
0014 %        fprintf('"[sortedElement, count] = elementCount(in)" produces the following output:\n');
0015 %        [sortedElement, count] = elementCount(in);
0016 %        fprintf('sortedElement = %s\n', '{''ab'', ''cd'', ''ef''}');
0017 %        fprintf('count = %s\n', '[3 2 1]');
0018 %
0019 %    Type "elementCount" for a self demo.
0020 
0021 %    Roger Jang, 19970327, 20071009
0022 
0023 if nargin<1, selfdemo; return, end
0024 
0025 if isnumeric(in)    % Numerical vector
0026     [m,n] = size(in);
0027     in1 = sort(in);
0028     in1(end+1)=in1(end)+1;
0029     index = find(diff(in1) ~= 0);
0030     sortedElement = in1(index);
0031     if n==1
0032         count = diff([0; index]);
0033     else
0034         count = diff([0, index]);
0035     end
0036 end
0037 
0038 if iscell(in)        % Cell string
0039     [m,n] = size(in);
0040     in1 = sort(in);
0041     in1{end+1}=[in1{end}, 'z'];
0042     index = find(strcmp(in1(1:end-1), in1(2:end))==0);
0043     sortedElement = in1(index);
0044     if n==1
0045         count = diff([0; index]);
0046     else
0047         count = diff([0, index]);
0048     end
0049 end
0050 
0051 % ====== Seld demo ======
0052 function selfdemo
0053 in = [5 1 5 5 7 9 9];
0054 fprintf('in = %s\n', mat2str(in));
0055 fprintf('"[sortedElement, count] = elementCount(in)" produces the following output:\n');
0056 [sortedElement, count] = elementCount(in);
0057 fprintf('sortedElement = %s\n', mat2str(sortedElement));
0058 fprintf('count = %s\n\n', mat2str(count));
0059 in = {'ab', 'cd', 'ef', 'ab', 'cd', 'ab'};
0060 fprintf('in = %s\n', '{''ab'', ''cd'', ''ef'', ''ab'', ''cd'', ''ab''}');
0061 fprintf('"[sortedElement, count] = elementCount(in)" produces the following output:\n');
0062 [sortedElement, count] = elementCount(in);
0063 fprintf('sortedElement = %s\n', '{''ab'', ''cd'', ''ef''}');
0064 fprintf('count = %s\n', '[3 2 1]');

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003