Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to create a transition matrix for a network. In order to do this, I need to sum down the column (the out degree), and then divide the column by the out degree in order to normalize it.

I am able to sum down the column. What I am unable to figure out how to do efficiently and easily is to divide the column by the sum.

L = {{0, 1, 0, 1, 0, 0, 0}, 
     {0, 0, 1, 1, 1, 0, 0}, 
     {0, 1, 0, 1, 0, 0, 0}, 
     {0, 0, 0, 0, 1, 0, 0}, 
     {1, 0, 0, 0, 0, 0, 0}, 
     {0, 0, 0, 0, 0, 0, 1}, 
     {0, 0, 0, 0, 0, 1, 0}};
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

If you need to do this with all columns, then:

Transpose[#/Total[#] & /@ Transpose[L]]
share|improve this answer
    
Where can I learn how to write these one liners that are so powerful? I never seem to fully understand the notation. –  olliepower Mar 29 '13 at 2:29
    
@user2200667 there is a tutorial collection Core Language - I'd start there. But also you can start by looking up in documentation symbols like /@ & # // etc. –  Vitaliy Kaurov Mar 29 '13 at 2:42
add comment

You can use Normalize with its second argument for this purpose:

(mat = Normalize[#, Total] & /@ Transpose@L // Transpose) // MatrixForm

Instead, if you were normalizing the rows by the sum of their elements, you could simply leave out the transposes and do

mat = Normalize[#, Total] & /@ L

or even

mat = #/Tr@#& /@ L

For your specific problem (transition matrix), you can use the new Markov process related functions in version 9 to get the transition matrix:

With[{m = DiscreteMarkovProcess[, L]},
   mat = MarkovProcessProperties[m, "TransitionMatrix"]
] // MatrixForm
share|improve this answer
    
+1 This is nice info. –  Vitaliy Kaurov Mar 29 '13 at 2:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.