Search code examples
rmatrixmatrix-multiplicationmultiplication

Multiply one column of matrix with fixed factor


this is probably a really trivial question, but I have not found a workable solution to it yet. There is a similar question posted here, but it did not get a workable answer, so I am grateful for any advice.

I have a simple 3x3 matrix and want to multiply the 2nd column by a fixed factor. Example:

m<-matrix(rep(c(1,2,3),1*3),nrow=3)
m
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3

If I try to do m[,2]*5, it gives me [1] 5 10 15

The result I would like looks like this:

     [,1] [,2] [,3]
[1,]    1    5    1
[2,]    2    10   2
[3,]    3    15   3

It can't be that difficult, can it?


Solution

  • Just assign it like this m[,2] <- m[,2]*5