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]]
Stephen Tucker
2008-Jun-18 15:01 UTC
[R] operations on all pairs of columns from two matrices
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.
Possibly Parallel Threads
- Very slow: using double apply and cor.test to compute correlation p.values for 2 matrices
- add/subtract matrices, ignoring NA or missing values
- Identifying common prefixes from a vector of words, and delete those prefixes
- selecting vector elements using matrices and combining the results
- compare two matrices