[R] operations on all pairs of columns from two matrices

Stephen Tucker brown_emu at yahoo.com
Wed Jun 18 17:01:56 CEST 2008


how about this?

m1 <- matrix(rep(1:3,each=5),ncol=3)
m2 <- matrix(1:15,ncol=3)

array(apply(m1,2,function(x,m) m-x,m2),dim=c(dim(m2),ncol(m1)))


----- Original Message ----
From: Daren Tan <daren76 at hotmail.com>
To: r-help at stat.math.ethz.ch
Sent: Wednesday, June 18, 2008 7:36:45 AM
Subject: [R] operations on all pairs of columns from two matrices



m1 <- matrix(rnorm(40), ncol=4)
m2 <- matrix(rnorm(40), ncol=4)

I would like to subtract first column of m1 from all columns of m2, subtract 2nd of m1 from all columns of m2, and so on. Obviously, I am not using the appropriate function outer(m1, m1, "-"), since the first column isn't all 0s. 

_________________________________________________________________


    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list