M.T.Charemza at warwick.ac.uk
2007-Nov-15 17:50 UTC
[R] Multiply each column of array by vector component
Hi, I've got an array, say with i,jth entry = A_ij, and a vector, say with jth entry= v_j. I would like to multiply each column of the array by the corresponding vector component, i,e. find the array with i,jth entry A_ij * v_j This seems so basic but I can't figure out how to do it without a loop. Any suggestions? Michal.
Benilton Carvalho
2007-Nov-15 17:51 UTC
[R] Multiply each column of array by vector component
?sweep b On Nov 15, 2007, at 12:50 PM, M.T.Charemza at warwick.ac.uk wrote:> Hi, > > I've got an array, say with i,jth entry = A_ij, and a vector, say with > jth > entry= v_j. I would like to multiply each column of the array by the > corresponding vector component, i,e. find the array with i,jth entry > > A_ij * v_j > > This seems so basic but I can't figure out how to do it without a loop. > Any suggestions? > > Michal. > > ______________________________________________ > 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.
Gabor Grothendieck
2007-Nov-15 18:00 UTC
[R] Multiply each column of array by vector component
On Nov 15, 2007 12:50 PM, <M.T.Charemza at warwick.ac.uk> wrote:> Hi, > > I've got an array, say with i,jth entry = A_ij, and a vector, say with jth > entry= v_j. I would like to multiply each column of the array by the > corresponding vector component, i,e. find the array with i,jth entry > > A_ij * v_j > > This seems so basic but I can't figure out how to do it without a loop. > Any suggestions? >Here are 4 ways: A <- matrix(1:6, 3) v <- 10:11 A %*% diag(v) A * rep(1, nrow(A)) %o% v t(t(A) * v) A * replace(A, TRUE, v[col(A)])
?sweep -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of > M.T.Charemza at warwick.ac.uk > Sent: Thursday, November 15, 2007 10:50 AM > To: r-help at r-project.org > Subject: [R] Multiply each column of array by vector component > > Hi, > > I've got an array, say with i,jth entry = A_ij, and a vector, > say with jth entry= v_j. I would like to multiply each column > of the array by the corresponding vector component, i,e. find > the array with i,jth entry > > A_ij * v_j > > This seems so basic but I can't figure out how to do it > without a loop. > Any suggestions? > > Michal. > > ______________________________________________ > 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. >
Henrique Dallazuanna
2007-Nov-15 18:01 UTC
[R] Multiply each column of array by vector component
If i understand your question, you can do: x <- matrix(1:10, 2) y <- sample(10,5) apply(x, 1, function(.x)mapply(y, .x, FUN="*")) On 15/11/2007, M.T.Charemza at warwick.ac.uk <M.T.Charemza at warwick.ac.uk> wrote:> Hi, > > I've got an array, say with i,jth entry = A_ij, and a vector, say with jth > entry= v_j. I would like to multiply each column of the array by the > corresponding vector component, i,e. find the array with i,jth entry > > A_ij * v_j > > This seems so basic but I can't figure out how to do it without a loop. > Any suggestions? > > Michal. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
On Thu, 2007-11-15 at 17:50 +0000, M.T.Charemza at warwick.ac.uk wrote:> Hi, > > I've got an array, say with i,jth entry = A_ij, and a vector, say with jth > entry= v_j. I would like to multiply each column of the array by the > corresponding vector component, i,e. find the array with i,jth entry > > A_ij * v_j > > This seems so basic but I can't figure out how to do it without a loop. > Any suggestions? > > Michal.If I understand you correctly: t(t(A) * v) set.seed(1) A <- matrix(sample(20), ncol = 2) v <- c(5, 2)> A[,1] [,2] [1,] 6 3 [2,] 8 2 [3,] 11 20 [4,] 16 10 [5,] 4 5 [6,] 14 7 [7,] 15 12 [8,] 9 17 [9,] 19 18 [10,] 1 13> t(t(A) * v)[,1] [,2] [1,] 30 6 [2,] 40 4 [3,] 55 40 [4,] 80 20 [5,] 20 10 [6,] 70 14 [7,] 75 24 [8,] 45 34 [9,] 95 36 [10,] 5 26 HTH, Marc Schwartz