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.

Right now I'm using this For loop to do an operation to each row of a $n \times n$ matrix, I was trying to figure out how to do this with Map or Take or something like that.

evecs = RandomReal[1, {5, 5}]
For[{i, 1, Length@evecs}, i++, evecs[[i, All]] = evecs[[i, All]]/Norm[evecs[[i, All]]]];
share|improve this question
    
Code doesn't run as-is. For[i=1,Length@evecs,i++... –  Corey Kelly Jun 24 '13 at 16:38
    
@CoreyKelly yes youre right, haha what is wrong? –  AimForClarity Jun 24 '13 at 16:39
1  
Is this what you want ? evecs2 = #/Norm[#] & /@ evecs –  Emy Jun 24 '13 at 16:40
    
@Emy oh that was silly, /@ already takes the rows it doesnt map to each element of the array, i think that is where i got confused. Thanks that is it, but i still wonder why the for loop doesnt work? –  AimForClarity Jun 24 '13 at 16:41
1  
Normalize /@ evecs –  Corey Kelly Jun 24 '13 at 16:41
show 6 more comments

1 Answer

up vote 8 down vote accepted

Using the basic approach of applying a function to the first level in a list (i.e. the 'rows' in a matrix) you have :

#/Norm[#] & /@ evecs

As CoreyKelly pointed out though, in your case you are just normalizing the elements, so you can apply the Normalize function :

Normalize /@ evecs

share|improve this answer
    
congrats on your first answer Emy!! –  AimForClarity Jun 24 '13 at 20:21
    
Thanks mate. And thanks to Corey for letting me have it. :) –  Emy Jun 24 '13 at 22:04
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.