0001 function [uniq_row, count] = rowCount(A)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 if nargin<1, selfdemo; return, end
0014 if isempty(A), uniq_row=[]; count=[]; return; end
0015
0016 cellstr_input = 0;
0017 if iscellstr(A),
0018 A = char(A);
0019 cellstr_input = 1;
0020 end
0021 term = sortrows(A);
0022 tmp1 = term;
0023 tmp1(end+1, :) = tmp1(end, :) + 1;
0024 tmp2 = tmp1(1:end-1, :) - tmp1(2:end, :);
0025 index = find(sum(abs(tmp2), 2) ~= 0);
0026 uniq_row = term(index, :);
0027 count = diff([0; index]);
0028
0029
0030
0031
0032
0033
0034
0035
0036 if cellstr_input,
0037 uniq_row = cellstr(uniq_row);
0038 end
0039
0040
0041 function selfdemo
0042 A = [ '一台';...
0043 '八爪'; ...
0044 '三光'; ...
0045 '人性'; ...
0046 '八爪'; ...
0047 '三光'; ...
0048 '八爪'];
0049 fprintf('Original string matrix:\n');
0050 disp(A);
0051 fprintf('After sorting and finding counts of unique rows:\n');
0052 [uniq_row, count] = feval(mfilename, A);
0053 for i = 1:length(count),
0054 fprintf('%s ==> %g\n', uniq_row(i, :), count(i));
0055 end
0056 fprintf('Original cell array of strings:\n');
0057 A = {'abc';'acd';'ad';'a';'bc';'ad';'acd';'acd'};
0058 disp(A);
0059 [uniq_row, count] = feval(mfilename, A);
0060 fprintf('After sorting and finding counts of unique rows:\n');
0061 for i = 1:length(count),
0062 fprintf('%s ==> %g\n', uniq_row{i}, count(i));
0063 end