


HISTND N-Dimensional histogram [NDCOUNTS, INDS] = HISTND(DATA, BINS); The DATA matrix is treated as a collection of D-dimensional row vectors and the entire range of the data is divided into BINS evenly spaced segments (default: 10) in and the density evaluated on a BINS^D grid. Rows with any NaN values are ignored. The counts of points on this grid are returned as a D-dimensional matrix. The optional second output containing the bin centers along any axis.


0001 function [ndcounts, inds] = histnd(data, bins) 0002 %HISTND N-Dimensional histogram 0003 % [NDCOUNTS, INDS] = HISTND(DATA, BINS); 0004 % The DATA matrix is treated as a collection of D-dimensional row 0005 % vectors and the entire range of the data is divided into BINS evenly 0006 % spaced segments (default: 10) in and the density evaluated on a BINS^D 0007 % grid. Rows with any NaN values are ignored. 0008 % 0009 % The counts of points on this grid are returned as a D-dimensional 0010 % matrix. The optional second output containing the bin centers along 0011 % any axis. 0012 0013 if (nargin == 1) % default values 0014 bins = 10; 0015 end 0016 0017 % Make the histogram grid 0018 D = size(data, 2); 0019 ndcounts = zeros(repmat(bins, 1, D)); 0020 0021 % We rescale and round the data to break it into (integer numbered) bins 0022 [data, oldmin, oldmax] = rescale(data, 0.5, bins+0.5); 0023 data = round(data); 0024 data(data == 0) = 1; 0025 data(data > bins) = bins; 0026 data = data(~any(isnan(data), 2),:); 0027 0028 % Now compute the density by treating each row of the scaled data as a coordinate in 0029 % the counts matrix and using sparse to do the histogramming. 0030 data = num2cell(data, 1); % convert coordinates to cells ... 0031 data = sub2ind(size(ndcounts), data{:}); % and then to unique tags. 0032 ndcounts = reshape(full(sparse(data, 1, 1, bins^D, 1)), size(ndcounts)); 0033 0034 if (nargout > 1) 0035 increment = (oldmax-oldmin)/bins; % bin width is uniform division of data range 0036 inds = (increment * (1:bins)) - increment/2 + oldmin; % compute bin centers 0037 end