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 written code that randomly generates a 3D vector of random magnitude. I now want to create a histogram of how many vectors lie in the concentric spherical shells (n*delta r, (n+1)delta r) starting from (0, delta r). The simplest way to do this is just find how many endpoints end up in each shell but I'm not sure how to go about this.

share|improve this question

2 Answers 2

This can be achieved easily. Let's say you have 200 random vectors

vecs = RandomReal[{-10, 10}, {200, 3}];

Mathematica graphics

The magnitude of a vector is just the length which can be calculated using Norm. Therefore, you Map the norm over all vectors and wrap Histogram around it

Histogram[Norm /@ vecs, 50]

Mathematica graphics

share|improve this answer
    
ah... well I'll upvote you anyway :) –  acl Oct 20 '12 at 22:05

You can count like this:

cnt[n_, dr_, lst_] := Length@Select[lst, n*dr < Norm[#] <= (n + 1)*dr &]

and then if you have 100 vectors:

vecList = RandomReal[{-10, 10}, {100, 3}];
cnt[12, 1, vecList]
Arrow[{{0, 0, 0}, #}] & /@ vecList // Graphics3D

Mathematica graphics

You can also make a histogram. Here, with bin widths 1:

Histogram[Norm /@ vecList, {1}]

Mathematica graphics

and here with bin widths .1:

Histogram[Norm /@ vecList, {.1}]

Mathematica graphics

share|improve this answer

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.