Normalizing a sparse matrix so that rows sum to 1
    3 views (last 30 days)
  
       Show older comments
    
    Ulrik William Nash
 on 26 Feb 2019
  
    
    
    
    
    Edited: Ulrik William Nash
 on 26 Feb 2019
            I have the following sparse matrix, which relates to a markov process. The parts of the matrix have been assembled sequentially, adding new entries to row, column, and probability one at a time, and only then creating
S = sparse(row,column,probability)
Because the sequential process involves aggregating probabilities from some states that are equaivalent
full(S)
results in a matrix, whose rows sum to more than one. What I wish to achieve is a normalization of each row in S, such that all rows sum to one. How can that be done by operating on S without needing to create the full matrix?
0 Comments
Accepted Answer
  John D'Errico
      
      
 on 26 Feb 2019
        
      Edited: John D'Errico
      
      
 on 26 Feb 2019
  
      WTP?
M = sprand(10000,10000,.00001);
mean(sum(M,2))
ans =
   (1,1)        0.050375
So M is large, sparse, and its rows sum to whatever they want to sum to.
M = M./sum(M,2);
[min(sum(M,2)),max(sum(M,2))]
ans =
   (1,1)               1
   (1,2)               1
So now normalized. The above will work properly in R2016b or later. I could have done the normalization by multiplying by a sparse diagonal matrix too, probably created using spdiags.
0 Comments
More Answers (0)
See Also
Categories
				Find more on Markov Chain Models in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
