cat2tbl.m

来自「一个非常实用的统计工具箱」· M 代码 · 共 45 行

M
45
字号
function t = cat2tbl(m1, m2)%CAT2TBL  Take category data and produce a table of counts.%%         table = cat2tbl(m1, m2)%%         It works with one input argument too.%%         See also CONTINCYif nargin < 2   if size(m1,2) == 2      m2 = m1(:,2);      m1 = m1(:,1);   else      m2 = ones(size(m1));   endendif size(m1,2) ~= 1 | size(m2,2) ~= 1   error('Column vectors expected as arguments')endif any([m1; m2] ~= round([m1; m2]))   error('Integers expected in input vectors');endif min(m1) == 0   m1 = m1 + 1;endif min(m2) == 0   m2 = m2 + 1;end% Here is a smart solution in Matlab 5:% t = full(sparse(m1,m2,1));% But we wat it to work in Octave too, % this solution assumes fortran indexing on:t = zeros(max(m1), max(m2));x = sort(m1 + (m2-1)*max(m1));i = find([1; diff(x)>0]);j = 1:length(x);count = diff([j(i), length(x)+1]);t(x(i)) = count;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?