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 <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
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@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.

______________________________________________
R-help@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.

Reply via email to