这是accumarray的工作:
r = [1 2 4 3 2];
c = [2 2 1 1 2];
%# for every r,c pair, sum up the occurences
%# the [4 3] is the array size you want
%# the zero is the value you want where there is no data
accumarray([r',c'],ones(length(r),1),[4 3],@sum,0)
ans =
0 1 0
0 2 0
1 0 0
1 0 0
请注意,如果结果数组有这么多零(即非常稀疏),稀疏可能是更好的选择,如@woodchips建议的那样
sparse(r,c,ones(size(r)),4,3)
ans =
(3,1) 1
(4,1) 1
(1,2) 1
(2,2) 2