文章 缘由
统计数组中各个元素数量是一个很常用的功能,但我试着用了MATLAB中自带的统计函数 tabulate :
但是发现了两个问题: 当元素中英文混杂时:
X = {'slandarer';'slandarer';'hikari';'hikari';'公众号';'公众号';
'CSDN';'CSDN';'CSDN'};
tabulate(X)
我们发现中英文混杂时输出会对不齐: 当元素为纯整数数值时:
X=[6,5,6];
tabulate(X)
即使元素没出现也会从1开始一直显示到最大值: 因而,为了解决这俩问题,我自行写了个元素统计类:statable
classdef statable
properties
% properties relationship:
% obj.Value=obj.Name(obj.Pos);
% obj.Percent=obj.Count/length(X);
% obj.Table=table(obj.Value,obj.Count,obj.Percent);
Value;Count;Percent;Table;Name;Pos
end
methods
% 构造函数
function obj=statable(X)
flag=false;
if isnumeric(X),flag=true;X=X(:);end
% 元素类型转换
SX=sort(X);OrgX=SX;
[SX,Xid]=grp2idx(SX);
obj.Name=Xid;
SX=SX(~isnan(SX));
DSX=diff(SX);
% 出现次数统计
DSX=find([DSX;1]);
obj.Pos=SX(DSX);
obj.Count=diff([0;DSX]);
obj.Percent=obj.Count/length(SX);
% 存储为table
obj.Value=obj.Name(obj.Pos);
if flag,obj.Value=unique(OrgX);end
obj.Table=table(obj.Value,obj.Count,obj.Percent);
end
% 输出函数
function show(obj)
fprintf(1,'%15s%10s%11s\n','Value','Count','Percent');
for i=1:length(obj.Pos)
tValue=obj.Name{obj.Pos(i)};
mspace=length(tValue)-sum(abs(tValue)>31&abs(tValue)
关注
打赏