# Vectors in a spherical shell

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.

-

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

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


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]


-
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


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

Histogram[Norm /@ vecList, {1}]


and here with bin widths .1:

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


-