Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
Normalize /@ evecs – Corey Kelly Jun 24 '13 at 16:41
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

Your Answer


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.