Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的稀疏(dgCMatrix)矩阵行归一化_R_Matrix_Sparse Matrix - Fatal编程技术网

R中的稀疏(dgCMatrix)矩阵行归一化

R中的稀疏(dgCMatrix)矩阵行归一化,r,matrix,sparse-matrix,R,Matrix,Sparse Matrix,我有一个很大的稀疏矩阵,称之为p: > str(P) Formal class 'dgCMatrix' [package "Matrix"] with 6 slots ..@ i : int [1:7868093] 4221 6098 8780 10313 11102 14243 20570 22145 24468 24977 ... ..@ p : int [1:7357] 0 0 269 388 692 2434 3662 4179 4205

我有一个很大的稀疏矩阵,称之为p:

 > str(P)
   Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
   ..@ i       : int [1:7868093] 4221 6098 8780 10313 11102 14243 20570 22145 24468 24977 ...
   ..@ p       : int [1:7357] 0 0 269 388 692 2434 3662 4179 4205 4256 ...
   ..@ Dim     : int [1:2] 1303967 7356
   ..@ Dimnames:List of 2
   .. ..$ : NULL
   .. ..$ : NULL
   ..@ x       : num [1:7868093] 1 1 1 1 1 1 1 1 1 1 ...
   ..@ factors : list()
我希望行正常化(比如,使用L-2范数)。。。(利用矢量循环)直接方法类似于:

> row_normalized_P <- P / rowSums(P^2)

>row\u normalized\u P

我想出了一个很好的解决方案(像往常一样,发布后大约15分钟:-/)


>row\u normalized\u P

我想出了一个很好的解决方案(像往常一样,发布后大约15分钟:-/)


>row\u normalized\u P对于列规格化,请记住交换乘法顺序:
P%*%Matrix::Diagonal(x=1/sqrt(Matrix::colSums(P^2))
对于列规格化,请记住交换乘法顺序:
P%*%Matrix::Diagonal(x=1/sqrt(Matrix::colSums(P^2))
> row_normalized_P <- Matrix::Diagonal(x = 1 / sqrt(Matrix::rowSums(P^2))) %*% P