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.

To normalize the last 2 or more columns of a matrix,

m = {{0, 0, 135, 276}, {0, 1, 762, 852}, {0, 2, 762, 852}, {1, 0, 162,
852}, {1, 1, 362, 852}, {1, 2, 262, 852}}

I can do that by

mm = Transpose@
 m /. {m[[All, 3]] -> m[[All, 3]]/(m[[All, 3]] + m[[All, 4]]), 
 m[[All, 4]] -> m[[All, 4]]/(m[[All, 3]] + m[[All, 4]])} // Transpose // MatrixForm

Or step by step

mt = Transpose[ m]

msum=mt[[3]]+mt[[4]]

m3=mt[[3]]/msum

m4=mt[[4]]/msum

mlast = Transpose[mt /. {mt[[3]] -> m3, mt[[4]] -> m4}]

are there any more elegant method in Mathematica?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

you normalize by the 3rd and the 4th element by the sum of these two elements, right?

Based on that, we might not want to use Normalize and divide directly:

#/{1, 1, #[[3]] + #[[4]], #[[3]] + #[[4]]} & /@ m
share|improve this answer
    
That's wonderful! thank Dirk. –  jiuyuxinshi May 2 '13 at 19:58

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.