[R] %*% in Matrix objects
Jose Quesada
quesada at gmail.com
Fri Jan 26 05:24:12 CET 2007
Dear R users,
I need to normalize a bunch of row vectors. At a certain point I need to divide a matrix by a vector of norms. I find that the behavior of Matrix objects differs from normal matrix objects. Example the following code examples differ only in xnormed changing from normal to Matrix object:
x = matrix(1:12,3,4)
x = as(x, "CsparseMatrix")
xnorms = sqrt(colSums(x^2))
(xnormed = t(x) * (1/xnorms))
This produces a "warning: coercing sparse to dense matrix for arithmetic
in: t(x) * 1/xnorms." but gets the result (a 4 x 3 matrix)
I want to stay in sparse format anyway (if it helps!) so I tried
x = matrix(1:12,3,4)
x = as(x, "CsparseMatrix")
xnorms = sqrt(colSums(x^2))
xnorms = as(xnorms, "CsparseMatrix")
(xnormed = t(x) * (1/xnorms))
But now, instead of a warning I get
"Error: Matrices must have same dimensions in t(x) * (1/xnorms)"
If I transpose the norms, the error dissapears, but the result is 1 x 4 (not 3 x 4 as before).
I suspect I'm facing the drop=T as before...
Also, it seems that in normal matrix objects %*% behaves the same as *, but in Matrix objects that is not the case.
What am I missing?
--
Thanks,
-Jose
--
Jose Quesada, PhD
Research fellow, Psychology Dept.
Sussex University, Brighton, UK
http://www.andrew.cmu.edu/~jquesada
More information about the R-help
mailing list