1 |
function [elts,nelt]=count(x) |
---|
2 |
%COUNT Elements of a matrix in set theoretic sense. |
---|
3 |
% COUNT(X) is a row vector containing the |
---|
4 |
% distinct elements of X. |
---|
5 |
% [ELTS,NELT] = COUNT(X) produces |
---|
6 |
% the elements of X in ELTS and |
---|
7 |
% the corresponding count of the elements in NELT. |
---|
8 |
% X is treated as 1 set and may contain NaN's and Inf's |
---|
9 |
% (which are counted also). Complex arrays as well as |
---|
10 |
% sparse matrices and text strings are handled properly. |
---|
11 |
% ELTS is sorted. |
---|
12 |
% Enter 'count' for a demo. |
---|
13 |
|
---|
14 |
% Author: J. Rodney Jee, rodjee@delphi.com, 28-JAN-95 |
---|
15 |
% Brian O. Blanton changed the name of this routine from |
---|
16 |
% ELEMENTS to COUNT for obvious FEM-related reasons. 30-Oct-95 |
---|
17 |
|
---|
18 |
if ( nargin == 0 ) % DEMO this function when no input. |
---|
19 |
disp('+++++++++++++++ DEMO of COUNT +++++++++++++++') |
---|
20 |
disp('GIVEN a set, say') |
---|
21 |
x = round( rand(4,6)*4 ) |
---|
22 |
disp('COUNT returns its') |
---|
23 |
[members,counts]=count(x); |
---|
24 |
members |
---|
25 |
disp('and their respective') |
---|
26 |
counts |
---|
27 |
disp('+++++++++++++++++ END of DEMO +++++++++++++++++') |
---|
28 |
return |
---|
29 |
end |
---|
30 |
|
---|
31 |
|
---|
32 |
% The key ideas of this method are to (1)sort the data, (2)take the |
---|
33 |
% differences of the sorted data and look for nonzeros in the |
---|
34 |
% differences which mark the ends of strings of the same values, (3) |
---|
35 |
% collect the values of step 2, and (4)use the indices of the |
---|
36 |
% jumps to tally the members. |
---|
37 |
|
---|
38 |
if (issparse(x)) % Check for sparse matrix. |
---|
39 |
nzeros=prod(size(x))-nnz(x); |
---|
40 |
x = nonzeros(x); % Required to be a column matrix. |
---|
41 |
else |
---|
42 |
if ( isstr(x) ) % Convert text strings to integer. |
---|
43 |
xstring=1; |
---|
44 |
x = abs(x); |
---|
45 |
x = x(:); |
---|
46 |
else |
---|
47 |
xstring=0; |
---|
48 |
nzeros=0; |
---|
49 |
x = x(:); |
---|
50 |
end |
---|
51 |
end |
---|
52 |
|
---|
53 |
indexf = finite(x); |
---|
54 |
xout = x( ~indexf ); % Set aside NaNs and Infs. |
---|
55 |
x = sort( x(indexf) ); % Step (1). |
---|
56 |
if ( isempty(x) ) |
---|
57 |
elts = []; |
---|
58 |
nelt = []; |
---|
59 |
elseif (length(x) == 1) |
---|
60 |
elts = x; |
---|
61 |
nelt = 1; |
---|
62 |
else |
---|
63 |
indjump = find(diff(x) ~= 0); % Step (2). |
---|
64 |
if ( length(indjump) == 0 ) |
---|
65 |
elts = x(1); |
---|
66 |
nelt = length(x); |
---|
67 |
else |
---|
68 |
elts = x(indjump); % Step (3). |
---|
69 |
elts = [elts.',x(length(x))]; |
---|
70 |
nelt = diff( [0, indjump.', length(x)] ); % Step (4). |
---|
71 |
end |
---|
72 |
end |
---|
73 |
|
---|
74 |
if (isempty(xout) & (nzeros==0)) |
---|
75 |
return |
---|
76 |
else % Append NaN's,Inf's, and 0's. |
---|
77 |
nnan = sum(isnan(xout)); |
---|
78 |
ninf = length(xout) - nnan; |
---|
79 |
if ( nnan > 0) |
---|
80 |
elts = [elts, NaN]; |
---|
81 |
nelt = [nelt, nnan]; |
---|
82 |
end |
---|
83 |
if ( ninf > 0) |
---|
84 |
elts = [elts, Inf]; |
---|
85 |
nelt = [nelt, ninf]; |
---|
86 |
end |
---|
87 |
if ( nzeros > 0) |
---|
88 |
elts = [elts, 0]; |
---|
89 |
nelt = [nelt, nzeros]; |
---|
90 |
end |
---|
91 |
end |
---|
92 |
|
---|
93 |
if (xstring) |
---|
94 |
elts = setstr(elts); |
---|
95 |
end |
---|