最近的一個作業要有這個輸出計算,直接拿來用的函數沒怎么找到,合計着就編一個吧,沒多少時間仔細編,但能用。
程序支持:Matlab2019b,以往的版本應該也能用。
function [d,zb1,ps] = pinshutongji(a,b) % a:輸入的矩陣 % b:要分成的組別,默認為10 % 輸出為:d(組別zb 頻數ps 累積lj(%)) zb1(組別數值矩陣double) ps(頻數數值矩陣double) % 效果為: % " 組別 " " 頻數" "累積(%)" % "-0.0040612 0.0032426" "44" "91.6667" % " 0.0032426 0.010546" "3" "97.9167" % " 0.010546 0.01785" "1" "100" if nargin==1 b = 10; end ps = zeros(b,1);lj = zeros(b,1); zb = (min(a):(max(a)-min(a))/b:max(a));zb = zb'; % 建立組別 for i = 1:1:length(a) % 計算頻數 temp = a(i); for j = 2:1:b+1 if temp<=zb(j,1) ps(j-1) = ps(j-1)+1; end end end asum = length(a); for i = 1:1:b % 計算累積 lj(i) = (ps(i,1)/asum)*100; end c = [" 組別 "," 頻數","累積(%)"]; zb1 = [zb(1:end-1,1),zb(2:end,1)]; zb2 = num2str(zb1); zb2 = cellstr(zb2); ps = [ps(1,1);ps(2:end,1)-ps(1:end-1,1)]; ps1 = num2cell(ps); lj = num2cell(lj); d = [c;zb2,ps1,lj]; end