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 have a List containing vectors. The coordinates are random numbers:

r = {RandomReal[{-1, 1}, n], RandomReal[{-1, 1}, n], RandomReal[{-1, 1}, n]};

Now I want to calculate the Norm of each Vector (into another List). Is this possible without a loop? Something like

Norm[r,2]

Where 2 is the dimension (columns) in which the norm should be calculated.

share|improve this question
    
Your question is unclear: what is n? Is each vector of length (dimension) n or of length (dimension) 3? –  murray Nov 13 '12 at 16:04
2  
Norm /@ Transpose[r] would do it, no? –  J. M. Nov 13 '12 at 16:08
    
n is the amount of vectors - sorry (in my example 1000) –  Vincent Nov 13 '12 at 16:12
    
And thanks J.M. your answer works like a charm (I searched something similar to "arrayfun" in Matlab). If you want you can post it as answer and I'll ccheck it as accepted answer –  Vincent Nov 13 '12 at 16:13
add comment

1 Answer 1

up vote 3 down vote accepted

One way is to Map[ ] the Norm function onto the list of vectors:

 Map[Norm, r]

If you think of r as being a matrix (with 3 rows and n columns, per the OPs code), this gives you a list, where each entry is the norm of the corresponding row in r. You can also use the infix notation for Map[ ] which is /@

Norm /@ r

If you want the Norm applied to the columns, then a Transpose[ ] is needed

 Norm/@ Transpose[r]
share|improve this answer
1  
OP's vectors are columns of the array and not the rows, so a preliminary Transpose[] is needed... –  J. M. Nov 13 '12 at 16:17
    
I had interpreted n as the length of each of the vectors (i.e., as the rows of the matrix r) with the goal of finding the norm of the three rows. If n is the number of vectors, and you want the norm of each column, then indeed you need to do Norm/@ Transpose[r] –  bill s Nov 13 '12 at 16:21
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.