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è)問(wèn)題:
當(dāng)元素中英文混雜時(shí):
X = {'slandarer';'slandarer';'hikari';'hikari';'公眾號(hào)';'公眾號(hào)'; 'CSDN';'CSDN';'CSDN'}; tabulate(X)
我們發(fā)現(xiàn)中英文混雜時(shí)輸出會(huì)對(duì)不齊:
當(dāng)元素為純整數(shù)數(shù)值時(shí):
X=[6,5,6]; tabulate(X)
即使元素沒(méi)出現(xiàn)也會(huì)從1開(kāi)始一直顯示到最大值:
因而,為了解決這倆問(wèn)題,我自行寫(xiě)了個(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); % 存儲(chǔ)為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';'公眾號(hào)';'公眾號(hào)'; '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)然,也可以通過(guò)如下方式獲取其他數(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); % 存儲(chǔ)為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';'公眾號(hào)';'公眾號(hào)'; '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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言 動(dòng)態(tài)分配數(shù)組案例詳解
這篇文章主要介紹了C語(yǔ)言 動(dòng)態(tài)分配數(shù)組案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08C++ std::shared_mutex讀寫(xiě)鎖的使用
本文主要介紹了C++ std::shared_mutex讀寫(xiě)鎖的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C++實(shí)現(xiàn)圖像目標(biāo)區(qū)裁剪ImageCropping
本文主要介紹了C++實(shí)現(xiàn)圖像目標(biāo)區(qū)裁剪ImageCropping,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06C++ OpenCV實(shí)戰(zhàn)之手寫(xiě)數(shù)字識(shí)別
這篇文章主要為大家詳細(xì)介紹了如何使用machine learning機(jī)器學(xué)習(xí)模塊進(jìn)行手寫(xiě)數(shù)字識(shí)別功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-08-08