In R, I want to set dgCMatrix non-zero value to a vector.
For example:
Here is a dgCMatrix:
M
3 x 4 sparse Matrix of class "dgCMatrix"
[1,] . 1 . 1
[2,] . . 1 .
[3,] . . 1 .
And here is a vector:
a <- c(1,2,3,4)
And then , I want to set the non-zero value to the vector by column . The result will be :
[1,] . 2 . 4
[2,] . . 3 .
[3,] . . 3 .
Thanks .
This should be pretty generally efficient:
Edit:
If
Mhas some values other than 0 and 1, you can instead use one of the following: