##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
x <- seq(1,4)
z <- x %+% -t(x)
x
z
x <- matrix(seq(1,6),ncol=2)
y <- matrix(seq(1,10),nrow=2)
z <- x %+% y
x
y
z
## The function is currently defined as
"%+%" <- 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)
y <- colSums(y)
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 return(xy) }
Run the code above in your browser using DataLab