Home > atmlab > math > hist2d.m

hist2d

PURPOSE ^

HIST2D calculates a 2-dimensional histogram

SYNOPSIS ^

function [counto,xco,yco] = hist2d(x,y,nx,ny)

DESCRIPTION ^

 HIST2D calculates a 2-dimensional histogram
    N = HIST2D(X,Y) bins the X and Y data into 10 equally spaced bins in
    both dimensions

    N = HIST2D(X,Y,M), where M is a scalar, bins the data into M equally
    spaced bins in both dimensions

    N = HIST2D(X,Y,B), where B is a vector, bins the data with centers
    specified by B 

    The number of bins or centers can be specified individually for either
    dimension using N = HIST2D(X,Y,NX,NY) or N = HIST2D(X,Y,BX,BY)

    [N,BX,BY] = HIST2D(...) also returns the positions of the bin centers
    as two matrices in BX and BY

    HIST2D(...) without output arguments produces a colormapped image plot
    of the 2d histogram

 EXAMPLE
   yin = randn(1,1000);
   xin = randn(1,1000);
   [n,x,y] = hist2d(xin,yin,11);
   imagesc(x(1,:),y(:,1),n); hold on; plot(xin,yin,'y.'); colorbar

 This is a downloaded function from somewhere, but I have optimized it a
 bit
 $Id: hist2d.m 8372 2013-04-24 06:23:43Z seliasson $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

hist2d.m

SOURCE CODE ^

0001 function [counto,xco,yco] = hist2d(x,y,nx,ny)
0002 % HIST2D calculates a 2-dimensional histogram
0003 %    N = HIST2D(X,Y) bins the X and Y data into 10 equally spaced bins in
0004 %    both dimensions
0005 %
0006 %    N = HIST2D(X,Y,M), where M is a scalar, bins the data into M equally
0007 %    spaced bins in both dimensions
0008 %
0009 %    N = HIST2D(X,Y,B), where B is a vector, bins the data with centers
0010 %    specified by B
0011 %
0012 %    The number of bins or centers can be specified individually for either
0013 %    dimension using N = HIST2D(X,Y,NX,NY) or N = HIST2D(X,Y,BX,BY)
0014 %
0015 %    [N,BX,BY] = HIST2D(...) also returns the positions of the bin centers
0016 %    as two matrices in BX and BY
0017 %
0018 %    HIST2D(...) without output arguments produces a colormapped image plot
0019 %    of the 2d histogram
0020 %
0021 % EXAMPLE
0022 %   yin = randn(1,1000);
0023 %   xin = randn(1,1000);
0024 %   [n,x,y] = hist2d(xin,yin,11);
0025 %   imagesc(x(1,:),y(:,1),n); hold on; plot(xin,yin,'y.'); colorbar
0026 %
0027 % This is a downloaded function from somewhere, but I have optimized it a
0028 % bit
0029 % $Id: hist2d.m 8372 2013-04-24 06:23:43Z seliasson $
0030 
0031 if nargin < 3
0032    nx = 10;
0033 end
0034 
0035 if nargin < 4
0036    ny = nx;
0037 end
0038 
0039 assert(isequal(size(x),size(y)),['atmlab:' mfilename ':badInput'],...
0040     'x and y must be same size');
0041 
0042 [~,xc] = hist(x,nx);
0043 [~,yc] = hist(y,ny);
0044 
0045 count = zeros(length(yc),length(xc));
0046 for i = 1:length(yc)
0047    if i == 1
0048       lbound = -Inf;
0049    else
0050       lbound = (yc(i-1) + yc(i)) / 2;
0051    end
0052    if i == length(yc)
0053       ubound = inf;
0054    else
0055       ubound = (yc(i) + yc(i+1)) /2;
0056    end
0057    count(i,:) = hist(x((y >= lbound) & (y < ubound)),xc);
0058 end
0059 
0060 [xc, yc] = meshgrid(xc, yc);
0061 
0062 if nargout == 0
0063    imagesc(xc(1,:),yc(:,1),count);
0064 else
0065    counto = count;
0066    xco = xc;
0067    yco = yc;
0068 end
0069 
0070 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005