function H=accumhist(I,W,D)
% accumhist - Just like accumarray but filters out rows with nans or infs
%
% accumhist ::
%    [[N,E]->(natural|nan|inf|-inf)] ~'N rows of E-dim array indices',
%    ([[N]->real] | real) ~'the weights associated with each row',
%    D:[[E]->natural]  ~'the size of the array to return'
% -> [[D]].
%
% Note that unlike with accumarray and other Matlab builtins, the size 
% argument D can be a single element, eg [M], indicating that a vector
% is desired. accumarry would require [M 1].

H = accumarray(I(all(isfinite(I),2),:),W,tosize(D));

