[R] Sparse (dgCMatrix) Matrix row-wise normalization

Stefan Evert stefanML at collocations.de
Thu May 4 20:23:16 CEST 2017


> On 4 May 2017, at 20:13, Murat Tasan <mmuurr at gmail.com> wrote:
> 
> The only semi-efficient method I've found around this is to `apply` across
> rows (more accurately through blocks of rows coerced into dense
> sub-matrices of P), but I'd like to try to remove the looping logic from my
> codebase if I can, and I'm wondering if perhaps there's a built-in in the
> Matrix package (that I'm just not aware of) that helps with this particular
> type of computation.

The "wordspace" package has an efficient C-level implementation for this purpose:

	P.norm <- normalize.rows(P)

which is a short-hand for

	P.norm <- scaleMargins(P, rows=1 / rowNorms(P, method="euclidean"))

Best,
Stefan


More information about the R-help mailing list