BINCOUNT 1-dimensional Bin Count CALL: [len,bin, val] = bincount(x,f); [len,bin] = bincount(x); len = vector with the number of equal values in x, i.e., len(k) = sum(x==bin(k)). bin = same values as in x, but with no repetitions, i.e., bin = unique(x). val = vector with the sum of the corresponding values i.e., val(k) = sum(f(x==bin(k))). x = vector of function arguments, e.g. an integer index vector. f = vector of function values, i.e., f(x). BINCOUNT counts the number of equal values in X, and optionally adds together any elements of F which have duplicate values of X into VAL. Example: N = 500; dx = 0.2; f = wraylrnd(1,N,1); ix = floor(f/dx)+1; [len,bin] = bincount(ix); plot((bin-.5)*dx,len/N/dx,'.') % 1D probability density plot bar((bin-.5)*dx,len/N/dx) % 1D probability density plot bar((bin-.5)*dx,len) % 1D Histogram See also sparse, histc.
Counting sorting | |
Difference and approximate derivative. | |
Display message and abort function. |
Parameter estimates for DIST2D data. | |
D-dimensional histogram using linear binning. |
001 function [len,bin,val] = bincount(x,f) 002 %BINCOUNT 1-dimensional Bin Count 003 % 004 % CALL: [len,bin, val] = bincount(x,f); 005 % [len,bin] = bincount(x); 006 % 007 % len = vector with the number of equal values in x, 008 % i.e., len(k) = sum(x==bin(k)). 009 % bin = same values as in x, but with no repetitions, 010 % i.e., bin = unique(x). 011 % val = vector with the sum of the corresponding values 012 % i.e., val(k) = sum(f(x==bin(k))). 013 % x = vector of function arguments, e.g. an integer index vector. 014 % f = vector of function values, i.e., f(x). 015 % 016 % BINCOUNT counts the number of equal values in X, and optionally 017 % adds together any elements of F which have duplicate values of X into VAL. 018 % 019 % Example: 020 % N = 500; dx = 0.2; 021 % f = wraylrnd(1,N,1); 022 % ix = floor(f/dx)+1; 023 % [len,bin] = bincount(ix); 024 % plot((bin-.5)*dx,len/N/dx,'.') % 1D probability density plot 025 % bar((bin-.5)*dx,len/N/dx) % 1D probability density plot 026 % bar((bin-.5)*dx,len) % 1D Histogram 027 % 028 % See also sparse, histc. 029 030 %Tested on: Matlab 5.3 031 %History: 032 % revised pab Dec2003 033 % renamed from binc to bincount 034 % by pab 14.08.2001 035 036 % check number of input arguments 037 error(nargchk(1, 2, nargin)); 038 039 isiz = size(x); 040 ldim = isiz > 1; 041 if sum(ldim) > 1 042 error('Input must be a vector.'); 043 end 044 045 % make sure input is a column vector 046 x = x(:); 047 048 % 049 try, 050 % Try counting sort 051 [x, ind] = csort(x); % assuming integer values 052 catch 053 [x, ind] = sort(x); % general 054 end 055 % Find indices to unique values 056 i = [ find(diff(x)) ; length(x) ]; 057 058 if nargout>1, 059 bin = x(i); % bin = unique(x); 060 end 061 i = [ 0 ; i ]; 062 063 len = diff(i); 064 065 % Make sure that the output is a vector in the same dimension as input 066 osiz = isiz; 067 osiz(ldim) = length(len); 068 len = reshape(len, osiz); 069 070 val = []; 071 if (nargin>1 & nargout>2), 072 if any(isiz~=size(f)), 073 error('The size of x and f must be equal!'), 074 end 075 f = f(:); 076 f = [ 0; cumsum(f(ind))]; 077 val = diff(f(i+1)); 078 val = reshape(val, osiz); 079 end 080 081 082 083 084 085 086 087
Comments or corrections to the WAFO group