[R] Sparse (dgCMatrix) Matrix row-wise normalization
Murat Tasan
mmuurr at gmail.com
Thu May 4 20:53:21 CEST 2017
Thanks, Stefan, I'll take a look!
Also, I figured out another solution (~15 minutes after posting :-/):
```
row_normalized_P <- Matrix::Diagonal(x = 1 / sqrt(Matrix::rowSums(P^2)))
%*% P
```
Cheers,
-m
On Thu, May 4, 2017 at 12:23 PM, Stefan Evert <stefanML at collocations.de>
wrote:
>
> > 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
[[alternative HTML version deleted]]
More information about the R-help
mailing list