统计数组中各个元素数量是一个很常用的功能,本文主要为大家介绍了如何利用Matlab优雅的统计集合中各元素出现的次数,感兴趣的可以了解一下
前言
统计数组中各个元素数量是一个很常用的功能,但我试着用了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)<127);
fprintf(['%',num2str(round(15-mspace)),'s'],tValue);
fprintf('%10d',obj.Count(i));
fprintf('%10.3f%%\n',100*obj.Percent(i));
end
end
end
end
使用方式
统计数字
X=[randi([0,10],[100000,1])];
T=statable(X);
T.show()
统计单词、名称
X = {'slandarer';'slandarer';'hikari';'hikari';'公众号';'公众号';
'CSDN';'CSDN';'CSDN'};
T=statable(X);
T.show()
统计字符
X=['Life is full of confusing and disordering Particular time,a particular location,',...
'Do the arranged thing of ten million time in the brain,Step by step ,',...
'the life is hard to avoid delicacy and stiffness No enthusiasm forever,',...
'No unexpected happening of surprising and pleasing So,',...
'only silently ask myself in mind Next happiness,when will come?']';
T=statable(X);
T.show()
当然,也可以通过如下方式获取其他数据:
T=statable(X);
T.Table
T.Value
T.Count
T.Percent
完整代码
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)<127);
fprintf(['%',num2str(round(15-mspace)),'s'],tValue);
fprintf('%10d',obj.Count(i));
fprintf('%10.3f%%\n',100*obj.Percent(i));
end
end
end
end
demo
% demo to test HistRate
X = {'slandarer';'slandarer';'hikari';'hikari';'公众号';'公众号';
'CSDN';'CSDN';'CSDN'};
T=statable(X);
T.show()
disp(' ')
X=[randi([0,10],[100000,1])];
T=statable(X);
T.show()
disp(' ')
X=['Life is full of confusing and disordering Particular time,a particular location,',...
'Do the arranged thing of ten million time in the brain,Step by step ,',...
'the life is hard to avoid delicacy and stiffness No enthusiasm forever,',...
'No unexpected happening of surprising and pleasing So,',...
'only silently ask myself in mind Next happiness,when will come?']';
T=statable(X);
T.show()
disp(' ')
T.Table
%T.Value
%T.Count
%T.Percent
到此这篇关于Matlab实现统计集合中各元素出现次数的示例代码的文章就介绍到这了,更多相关Matlab统计元素出现次数内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:Matlab实现统计集合中各元素出现次数的示例代码


猜你喜欢
- ubuntu下C/C++获取剩余内存 2023-09-18
- Easyx实现扫雷游戏 2023-02-06
- C语言qsort()函数的使用方法详解 2023-04-26
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- Qt计时器使用方法详解 2023-05-30
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- C++ 数据结构超详细讲解顺序表 2023-03-25
- C语言详解float类型在内存中的存储方式 2023-03-27
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30