[R] %*% in Matrix objects

From: Jose Quesada <quesada_at_gmail.com>
Date: Fri 26 Jan 2007 - 04:24:12 GMT

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?


Jose Quesada, PhD
Research fellow, Psychology Dept.
Sussex University, Brighton, UK

R-help@stat.math.ethz.ch mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Fri Jan 26 15:28:45 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 26 Jan 2007 - 10:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.