Skip to contents

This function normalizes each row of a sparse matrix so that the row sums to 1. This is often required before diffusion or spillover modeling to create a row-stochastic matrix.

Usage

normalize_rows(W, verbose = TRUE)

Arguments

W

A sparse matrix of class `dgCMatrix`, typically an adjacency or TOM matrix.

verbose

Logical. If TRUE, prints the number of rows that were corrected (i.e., had a sum of zero).

Value

A row-normalized sparse matrix of the same dimensions and class.

Details

If any row has a sum of zero (i.e., disconnected node), the function replaces the diagonal entry for that row with 1, effectively preserving its identity and avoiding division by zero.

Examples

if (FALSE) { # \dontrun{
W_norm <- normalize_rows(W)
} # }