3
$\begingroup$

I have two arrays describing a 1-dimensional mass distribution. The first array, $x$, are the (un-sorted, and un-evenly distributed) x-coordinates; the second, $m$, the corresponding masses. I would like to bin the data, and obtain the total masses contained in each bin and plot them out. My question is: how to do that?

I could first get the sorted $x$ array, $x'$, and get the corresponding $m'$, then Do-loop to sum every few $m'$ elements. But it seems a question of weighted histogram, so I guess there might be a simpler solution?

$\endgroup$
2
$\begingroup$

Already voted to close. Anyway. Using @kguler's answer (better than mine there)

SeedRandom[42];
n = 20;
pos = RandomInteger[{1, 100}, n];
mass = RandomInteger[{1, 3}, n];
wd = WeightedData[pos, mass];
binlimits = Range[Min@#, Max@#, 10] &@pos;
dist = HistogramDistribution[wd, {binlimits}];
tm = Total[mass] Normalize[dist["PDFValues"], Total];
RectangleChart[Transpose[{Differences@binlimits, tm}], 
 Epilog -> 
  Transpose[{PointSize[#/Total@mass/2] & /@ mass, 
    Point /@ Array[{pos[[#]] - Min@pos, 8} &, Length@pos]}]]  

Mathematica graphics

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.