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

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

因而,為了解決這倆問題,我自行寫了個元素統(tǒng)計類:statable
工具函數(shù)類
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
% 構(gòu)造函數(shù)
function obj=statable(X)
flag=false;
if isnumeric(X),flag=true;X=X(:);end
% 元素類型轉(zhuǎn)換
SX=sort(X);OrgX=SX;
[SX,Xid]=grp2idx(SX);
obj.Name=Xid;
SX=SX(~isnan(SX));
DSX=diff(SX);
% 出現(xiàn)次數(shù)統(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
% 輸出函數(shù)
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)計數(shù)字
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()
當然,也可以通過如下方式獲取其他數(shù)據(jù):
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
% 構(gòu)造函數(shù)
function obj=statable(X)
flag=false;
if isnumeric(X),flag=true;X=X(:);end
% 元素類型轉(zhuǎn)換
SX=sort(X);OrgX=SX;
[SX,Xid]=grp2idx(SX);
obj.Name=Xid;
SX=SX(~isnan(SX));
DSX=diff(SX);
% 出現(xiàn)次數(shù)統(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
% 輸出函數(shù)
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實現(xiàn)統(tǒng)計集合中各元素出現(xiàn)次數(shù)的示例代碼的文章就介紹到這了,更多相關Matlab統(tǒng)計元素出現(xiàn)次數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++實現(xiàn)圖像目標區(qū)裁剪ImageCropping
本文主要介紹了C++實現(xiàn)圖像目標區(qū)裁剪ImageCropping,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06
C++ OpenCV實戰(zhàn)之手寫數(shù)字識別
這篇文章主要為大家詳細介紹了如何使用machine learning機器學習模塊進行手寫數(shù)字識別功能,文中的示例代碼講解詳細,感興趣的可以了解一下2022-08-08

