Skip to content
Prev 314621 / 398502 Next

Working with Matrix

Hello,

Using part of your code, it's possible to do without Reduce, have foo 
(fun, below) do the job.

fun <- function(x, y, FUN = `+`){
     if(nrow(x) < nrow(y)){
         nas <- matrix(NA, ncol = ncol(x), nrow = nrow(y) - nrow(x))
         x <- rbind(x, nas)
     }else{
         nas <- matrix(NA, ncol = ncol(y), nrow = nrow(x) - nrow(y))
         y <- rbind(y, nas)
     }
     FUN(x, y)
}

fun(Mat1, Mat2)
fun(Mat1, Mat2, `*`)  # Note the back quotes


Hope this helps,

Rui Barradas
Em 04-01-2013 12:06, Jorge I Velez escreveu: