binmat <- function(len, nbin) { # Function to make matrix for binning by matrix multiplication # len is the length of the input vector # nbin is the number by which to bin # This version puts out vector of shorter length, makes means for end # that doesn't fit within binning # To bin, multipy binmat %*% input # # AH 1/2008 # Set number of unique rows and weighting for mean n <- len%/%nbin w <- 1./nbin # Make empty binning matrix nend <- len%%nbin X <- matrix(0, nrow=ifelse(nend==0, n, n+1), ncol=len) # Fill nonzero elements into matrix for (i in 1:n){ X[i, ((i-1)*nbin+1):(i*nbin)] <- w } # Deal with incomplete bin at end, if needed if (nend!=0) { w <- 1./nend X[n+1, (len-nend+1):len] <- w } return(X) }