binmat2 <- 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 same length with repeated elements, # makes means for end that doesn't fit within binning # To bin, multipy binmat2 %*% input # # AH 1/2008 # Set number of unique rows and weighting for mean n <- len%/%nbin w <- 1./nbin # Make empty binning matrix X <- matrix(0, nrow=len, ncol=len) # Fill nonzero elements into matrix for (i in 1:n){ colindex <- (i-1)*nbin bindex <- colindex+1 eindex <- colindex + nbin for (j in 1:nbin){ X[colindex+j, bindex:eindex] <- w } } # Deal with incomplete bins at end, if needed nend <- len%%nbin if (nend!=0) { w <- 1./nend tmp <- len-nend+1 for (i in tmp:len) { X[i, tmp:len] <- w } } return(X) }