Michael
2006-Mar-06 23:10 UTC
[R] is there a way to let R do smart matrix-vector operation?
Hi all, I want to substract vector B from A's each column... how can R do that smartly without a loop?> A=matrix(c(2:7), 2, 3) > A[,1] [,2] [,3] [1,] 2 4 6 [2,] 3 5 7> B=matrix(c(1, 2), 2, 1) > B[,1] [1,] 1 [2,] 2> A-BError in A - B : non-conformable arrays [[alternative HTML version deleted]]
Marc Schwartz (via MN)
2006-Mar-06 23:20 UTC
[R] is there a way to let R do smart matrix-vector operation?
On Mon, 2006-03-06 at 15:10 -0800, Michael wrote:> Hi all, > > I want to substract vector B from A's each column... how can R do that > smartly without a loop? > > > A=matrix(c(2:7), 2, 3) > > A > [,1] [,2] [,3] > [1,] 2 4 6 > [2,] 3 5 7 > > B=matrix(c(1, 2), 2, 1) > > B > [,1] > [1,] 1 > [2,] 2 > > A-B > Error in A - B : non-conformable arrays> apply(A, 2, "-", B)[,1] [,2] [,3] [1,] 1 3 5 [2,] 1 3 5 You can use apply() on column-wise operations such as this. See ?apply for more information. HTH, Marc Schwartz
Liaw, Andy
2006-Mar-06 23:20 UTC
[R] is there a way to let R do smart matrix-vector operation?
It's rarely necessary to have a vector in matrix form. In this case, it actually make things harder. If B had been a vector, you would have gotten:> A=matrix(c(2:7), 2, 3) > B=matrix(c(1, 2), 2, 1) > A - as.vector(B)[,1] [,2] [,3] [1,] 1 3 5 [2,] 1 3 5 Andy From: Michael> > Hi all, > > I want to substract vector B from A's each column... how can > R do that smartly without a loop? > > > A=matrix(c(2:7), 2, 3) > > A > [,1] [,2] [,3] > [1,] 2 4 6 > [2,] 3 5 7 > > B=matrix(c(1, 2), 2, 1) > > B > [,1] > [1,] 1 > [2,] 2 > > A-B > Error in A - B : non-conformable arrays > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Berton Gunter
2006-Mar-06 23:24 UTC
[R] is there a way to let R do smart matrix-vector operation?
Well, I'm not sure what's "smart" and what's "dumb." As Mark said, a standard prescription for this sort of thing is to use apply() type calls -- but that actually is still using a loop, though generally very efficiently. If you want to avoid even that sort of hidden looping then try: matrix(as.vector(A)-as.vector(B),nr=nrow(A)) If B is a vector, not a matrix, the as.vector(B) cast isn't necessary. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Michael > Sent: Monday, March 06, 2006 3:11 PM > To: R-help at stat.math.ethz.ch > Subject: [R] is there a way to let R do smart matrix-vector operation? > > Hi all, > > I want to substract vector B from A's each column... how can R do that > smartly without a loop? > > > A=matrix(c(2:7), 2, 3) > > A > [,1] [,2] [,3] > [1,] 2 4 6 > [2,] 3 5 7 > > B=matrix(c(1, 2), 2, 1) > > B > [,1] > [1,] 1 > [2,] 2 > > A-B > Error in A - B : non-conformable arrays > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Anders Nielsen
2006-Mar-06 23:27 UTC
[R] is there a way to let R do smart matrix-vector operation?
Hi Michael, Try: A-as.vector(B) Cheers, Anders. On Monday 06 March 2006 01:10 pm, Michael wrote:> Hi all, > > I want to substract vector B from A's each column... how can R do > that smartly without a loop? > > > A=matrix(c(2:7), 2, 3) > > A > > [,1] [,2] [,3] > [1,] 2 4 6 > [2,] 3 5 7 > > > B=matrix(c(1, 2), 2, 1) > > B > > [,1] > [1,] 1 > [2,] 2 > > > A-B > > Error in A - B : non-conformable arrays > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
Gabor Grothendieck
2006-Mar-07 00:55 UTC
[R] is there a way to let R do smart matrix-vector operation?
The following are nearly identical to what others have already written but just in case: A - c(B) or A - B[,1] or if B were already a vector, b, in the first place, rather than a matrix: b <- 1:2 A - b On 3/6/06, Michael <comtech.usa at gmail.com> wrote:> Hi all, > > I want to substract vector B from A's each column... how can R do that > smartly without a loop? > > > A=matrix(c(2:7), 2, 3) > > A > [,1] [,2] [,3] > [1,] 2 4 6 > [2,] 3 5 7 > > B=matrix(c(1, 2), 2, 1) > > B > [,1] > [1,] 1 > [2,] 2 > > A-B > Error in A - B : non-conformable arrays > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Robin Hankin
2006-Mar-07 08:07 UTC
[R] is there a way to let R do smart matrix-vector operation?
One can always use the often-overlooked sweep(): > a <- matrix(2:7,2,3) > b <- matrix(1:2,2,1) > sweep(a,1,b) [,1] [,2] [,3] [1,] 1 3 5 [2,] 1 3 5 > best rksh On 6 Mar 2006, at 23:10, Michael wrote:> Hi all, > > I want to substract vector B from A's each column... how can R do that > smartly without a loop? > >> A=matrix(c(2:7), 2, 3) >> A > [,1] [,2] [,3] > [1,] 2 4 6 > [2,] 3 5 7 >> B=matrix(c(1, 2), 2, 1) >> B > [,1] > [1,] 1 > [2,] 2 >> A-B > Error in A - B : non-conformable arrays > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting- > guide.html-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
(Ted Harding)
2006-Mar-07 08:34 UTC
[R] is there a way to let R do smart matrix-vector operation
On 06-Mar-06 Michael wrote:> Hi all, > > I want to substract vector B from A's each column... how can R do that > smartly without a loop? > >> A=matrix(c(2:7), 2, 3) >> A > [,1] [,2] [,3] > [1,] 2 4 6 > [2,] 3 5 7 >> B=matrix(c(1, 2), 2, 1) >> B > [,1] > [1,] 1 > [2,] 2 >> A-B > Error in A - B : non-conformable arraysWhat a lot of answers to this query! Also this one (which I adopted following someone's useful response to a query of mine a very long time ago -- sorry I can't now trace who it was): ## Binary operators %+.%, %.+% to add vector ## or kx1 or 1xk matrix to cols or rows of a matrix "%+.%"<-function(X,x){sweep(X,1,x,"+")} ## Adds to cols "%.+%"<-function(X,x){sweep(X,2,x,"+")} ## Adds to rows So A %+.% x would add the elements of x to cols of A, or A %.+% y would add the elements of y to rows of A, provided the length of x is the number of rows in A or the length or y is the number of columns. These operators can easily be extended to %-.%, %.-%, %*.%, %.*%, %/.%, %./% (in an obvious notation). I have the above operators set up in my .Rprofile (from which the above code is copied). The only thing wrong with them is that they are traps for typophiliacs (like me ... ). Best wishes to all, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 07-Mar-06 Time: 08:34:03 ------------------------------ XFMail ------------------------------