[R] Multiply each column of array by vector component
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Nov 15 19:00:14 CET 2007
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)])
More information about the R-help
mailing list