2d - fast 2dimensional histograming in matlab -
i have written 2d histogram algorithm 2 matlab vectors. unfortunately, cannot figure out how vectorize it, , order of magnitude slow needs. here have:
function [ result ] = hist2d( vec0, vec1 ) %hist2d takes 2 vectors, , computes 2 dimensional histogram % of images. assumes vectors non-negative, , bins % integers. % % outputs % result - % size(result) = 1 + [max(vec0) max(vec1)] % result(i,j) = number of pixels have value % i-1 in vec0 , value j-1 in vec1. result = zeros(max(vec0)+1, max(vec1)+1); fvec0 = floor(vec1)+1; fvec1 = floor(vec0)+1; % ugh, gross, there has better way... = 1 : size(fvec0); result(fvec0(i), fvec1(i)) = 1 + result(fvec0(i), fvec1(i)); end end
thoughts?
thanks!! john
here version 2d histogram:
%# random data x = randn(2500,1); y = randn(2500,1)*2; %# bin centers (integers) xbins = floor(min(x)):1:ceil(max(x)); ybins = floor(min(y)):1:ceil(max(y)); xnumbins = numel(xbins); ynumbins = numel(ybins); %# map x/y values bin indices xi = round( interp1(xbins, 1:xnumbins, x, 'linear', 'extrap') ); yi = round( interp1(ybins, 1:ynumbins, y, 'linear', 'extrap') ); %# limit indices range [1,numbins] xi = max( min(xi,xnumbins), 1); yi = max( min(yi,ynumbins), 1); %# count number of elements in each bin h = accumarray([yi(:) xi(:)], 1, [ynumbins xnumbins]); %# plot 2d histogram imagesc(xbins, ybins, h), axis on %# axis image colormap hot; colorbar hold on, plot(x, y, 'b.', 'markersize',1), hold off
note removed "non-negative" restriction, kept integer bin centers (this changed dividing range equally-sized specified number of bins instead "fractions").
this inspired @steveeddins blog post.
Comments
Post a Comment