1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
function table = tabulate2(x)
isnum = isnumeric(x);
if isnum
if min(size(x)) > 1,
error('stats:tabulate:InvalidData','Requires a vector input.');
end
y = x(~isnan(x));
else
y = x;
end
if ~isnum || any(y ~= round(y)) || any(y < 1);
docell = true;
[y,yn] = grp2idx(y);
maxlevels = length(yn);
else
docell = false;
maxlevels = max(y);
%yn = cellstr(num2str((1:maxlevels)'));
end
[counts values] = hist(y,(1:maxlevels));
total = sum(counts);
percents = 100*counts./total;
if nargout == 0
if docell
width = max(cellfun('length',yn));
width = max(5, min(50, width));
else
width = 5;
end
% Create format strings similar to: ' %5s %15d %6.2f%%\n'
fmt1 = sprintf(' %%%ds %%15d %%6.2f%%%%\n',width);
fmt2 = sprintf(' %%%ds %%5s %%6s\n',width);
fprintf(1,fmt2,'Value','Count','Percent');
if docell
for j=1:maxlevels
fprintf(1,fmt1,yn{j},counts(j),percents(j));
end
else
fprintf(1,' %15d %15d %6.2f%%\n',[values' counts' percents']');
end
else
if ~docell
table = [values' counts' percents'];
elseif isnum
table = [str2num(char(yn{:})) counts' percents'];
else
table = [yn num2cell([counts' percents'])];
end
end |
Partager