Matlab實現統(tǒng)計集合中各元素出現次數的示例代碼
前言
統(tǒng)計數組中各個元素數量是一個很常用的功能,但我試著用了MATLAB中自帶的統(tǒng)計函數 tabulate:
但是發(fā)現了兩個問題:
當元素中英文混雜時:
X = {'slandarer';'slandarer';'hikari';'hikari';'公眾號';'公眾號';
'CSDN';'CSDN';'CSDN'};
tabulate(X)我們發(fā)現中英文混雜時輸出會對不齊:

當元素為純整數數值時:
X=[6,5,6]; tabulate(X)
即使元素沒出現也會從1開始一直顯示到最大值:

因而,為了解決這倆問題,我自行寫了個元素統(tǒng)計類: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);
% 出現次數統(tǒng)計
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使用方式
統(tǒng)計數字
X=[randi([0,10],[100000,1])]; T=statable(X); T.show()

統(tǒng)計單詞、名稱
X = {'slandarer';'slandarer';'hikari';'hikari';'公眾號';'公眾號';
'CSDN';'CSDN';'CSDN'};
T=statable(X);
T.show()
統(tǒng)計字符
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);
% 出現次數統(tǒng)計
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
enddemo
% 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實現統(tǒng)計集合中各元素出現次數的示例代碼的文章就介紹到這了,更多相關Matlab統(tǒng)計元素出現次數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

