Matlab實(shí)現(xiàn)統(tǒng)計(jì)集合中各元素出現(xiàn)次數(shù)的示例代碼
前言
統(tǒng)計(jì)數(shù)組中各個(gè)元素?cái)?shù)量是一個(gè)很常用的功能,但我試著用了MATLAB中自帶的統(tǒng)計(jì)函數(shù) tabulate:
但是發(fā)現(xiàn)了兩個(gè)問題:
當(dāng)元素中英文混雜時(shí):
X = {'slandarer';'slandarer';'hikari';'hikari';'公眾號';'公眾號'; 'CSDN';'CSDN';'CSDN'}; tabulate(X)
我們發(fā)現(xiàn)中英文混雜時(shí)輸出會對不齊:
當(dāng)元素為純整數(shù)數(shù)值時(shí):
X=[6,5,6]; tabulate(X)
即使元素沒出現(xiàn)也會從1開始一直顯示到最大值:
因而,為了解決這倆問題,我自行寫了個(gè)元素統(tǒng)計(jì)類: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)計(jì) 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)計(jì)數(shù)字
X=[randi([0,10],[100000,1])]; T=statable(X); T.show()
統(tǒng)計(jì)單詞、名稱
X = {'slandarer';'slandarer';'hikari';'hikari';'公眾號';'公眾號'; 'CSDN';'CSDN';'CSDN'}; T=statable(X); T.show()
統(tǒng)計(jì)字符
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()
當(dāng)然,也可以通過如下方式獲取其他數(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)計(jì) 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
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
到此這篇關(guān)于Matlab實(shí)現(xiàn)統(tǒng)計(jì)集合中各元素出現(xiàn)次數(shù)的示例代碼的文章就介紹到這了,更多相關(guān)Matlab統(tǒng)計(jì)元素出現(xiàn)次數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)圖像目標(biāo)區(qū)裁剪ImageCropping
本文主要介紹了C++實(shí)現(xiàn)圖像目標(biāo)區(qū)裁剪ImageCropping,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06C++ OpenCV實(shí)戰(zhàn)之手寫數(shù)字識別
這篇文章主要為大家詳細(xì)介紹了如何使用machine learning機(jī)器學(xué)習(xí)模塊進(jìn)行手寫數(shù)字識別功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-08-08