normalization - In Julia, How can I column-normalize a sparse matrix? -
normalization - In Julia, How can I column-normalize a sparse matrix? -
if have constructed sparse matrix using sparse(i, j, k) constructor, how can normalize columns of matrix (so each column sums 1)? cannot efficiently normalize entries before create matrix, help appreciated. thanks!
the easiest way broadcasting partition sum of columns:
julia> = sprand(4,5,.5) a./sum(a,1) 4x5 array{float64,2}: 0.0 0.0989976 0.0 0.0 0.0795486 0.420754 0.458653 0.0986313 0.0 0.0 0.0785525 0.442349 0.0 0.856136 0.920451 0.500693 0.0 0.901369 0.143864 0.0
… looks hasn't been optimized sparse matrices yet, , falls total matrix. simple loop iterate on columns trick:
julia> (col,s) in enumerate(sum(a,1)) s == 0 && go on # "normalized" column sum of 0 like? a[:,col] = a[:,col]/s end 4x5 sparse matrix 12 float64 entries: [2, 1] = 0.420754 [3, 1] = 0.0785525 [4, 1] = 0.500693 [1, 2] = 0.0989976 [2, 2] = 0.458653 [3, 2] = 0.442349 [2, 3] = 0.0986313 [4, 3] = 0.901369 [3, 4] = 0.856136 [4, 4] = 0.143864 [1, 5] = 0.0795486 [3, 5] = 0.920451 julia> sum(a,1) 1x5 array{float64,2}: 1.0 1.0 1.0 1.0 1.0
this works exclusively within sparse matrices , done in-place (although still allocating new sparse matrices each column slice).
matrix normalization sparse-matrix julia-lang
Comments
Post a Comment