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

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

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


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.