R/matrix.addition.R

#matrix.addition 
"%+%" <- function(x,y) { 
 if(!is.matrix(x)) {
	if(is.vector(x)) {x <- as.matrix(x)} else stop("x must be either a vector or a matrix")}
if(!is.matrix(y)) {
	if(is.vector(y)) {y <- as.matrix(y)} else stop("y must be either a vector or a matrix")}
n.x <- dim(x)[1]
n.y <- dim(y)[2]
n.k <- dim(x)[2]
if (n.k != dim(y)[1]) {warning("Matrices should be comparable")}
#first find sum vectors
x <- rowSums(x,na.rm=FALSE)
y <- colSums(y,na.rm=FALSE)
one <- as.vector(rep(1,n.y))  #to duplicate x n.y times
one.y <- as.vector(rep(1,n.x)) #to duplicate y n.x times
xy  <- x %*% t(one) + t(y %*% t(one.y) )  #sum the vectors in a rectangular array
  return(xy) }
  
 "tr" <- 
function(m) {
if (!is.matrix(m) |(dim(m)[1] != dim(m)[2]) ) stop ("m must be a square matrix")
return(sum(diag(m),na.rm=TRUE)) }

Try the psych package in your browser

Any scripts or data that you put into this service are public.

psych documentation built on May 7, 2018, 1:04 a.m.