BinCounts
Details
- BinCounts drops elements whose values do not correspond to real numbers.
- The following bin-width specifications binspec can be given:
-
dx bins of width dx » {xmin,xmax,dx} bins of width dx from xmin to xmax » {{b1,b2,…}} the intervals [b1,b2), [b2,b3), … » xbins,ybins,… bin specifications for multivariate data understood as {{x1,y1,…},{x2,y2, …},…} » - BinCounts[data,dx] takes the bin boundaries to be integer multiples of dx, with the first bin starting at Ceiling[Min[data]-dx,dx] and the last bin ending at Floor[Max[data]+dx,dx].
-
- BinCounts[data] is equivalent to BinCounts[data,1].
- In BinCounts[data,{xmin,xmax,dx}], elements are counted in bin i when their values satisfy
. -
- BinCounts[data,{xmin,xmax}] is equivalent to BinCounts[data,{xmin,xmax,1}].
- In BinCounts[data,{{b1,b2,…}}], elements are counted in bin i when their values satisfy
. -
- In the form BinCounts[data,{{b1,b2,…}}], the bi at each end can be -Infinity and +Infinity.
- If the bi do not form an increasing sequence, they are automatically sorted by BinCounts.
- If data consists of length-n sublists, then n bin specifications must be given, and BinCounts[data,…] yields an array of depth n.
- The data can have the following forms and interpretations:
-
{x1,x2,…} list of real numbers or quantities » {{x1,y1,…},{x2,y2,…},…} list of vectors of the same dimension » SparseArray an array equivalent to Normal[data] » QuantityArray array of column-compatible quantities » TimeSeries, TemporalData,… vector or array of values (the time stamps ignored) »
Examples
open all close allBasic Examples (3)
Count the number of elements in bins of a specified width:
BinCounts[{1, 3, 2, 1, 4, 5, 6, 2}, 2]Count the number of elements in bins of width 1 from 0 to 10:
BinCounts[{1, 3, 2, 1, 4, 5, 6, 2}, {0, 10, 1}]Count the number of elements in a sequence of ranges:
BinCounts[{1, 3, 2, 1, 4, 5, 6, 2}, {{-Infinity, 2, 5, 7, Infinity}}]Scope (11)
Basic Uses (8)
Count squares mod 3 and 5 in two-dimensional unit bins:
data = Table[Mod[i ^ 2, {3, 5}], {i, 100}];
Dimensions[data]BinCounts[data, 1, 1]Count random pairs in bins of width 0.25 in both dimensions:
data = RandomReal[{-1, 1}, {1000, 2}];
Dimensions[data]BinCounts[data, {-1, 1, .25}, {-1, 1, .25}]Count multidimensional data in ranges:
data = RandomReal[{-1, 1}, {1000, 2}];BinCounts[data, {{-2, .3, 2}}, {{-1, -.4, .2, 1}}]BinCounts[RandomReal[{-1, 1}, {1000, 5}]]Count binned data, ignoring values that are not real:
BinCounts[{1.5, 3, a, 2.5, 1, I}, 2]Count binned data of any precision:
BinCounts[{1.5, 3, N[3, 20], 2.5, 1, E}, 2]SparseArray data can be used just like dense arrays:
data = SparseArray[{{1, 1} -> 5, {3, 2} -> 4}, {100, 2}]BinCounts[data]sp = SparseArray[{{i_, i_} :> i, {i_, j_} /; j == i + 1 :> i - 1}, {100, 10}, 3]BinCounts[sp]Count the values of data in a time series:
ts = TemporalData[TimeSeries, {{{-0.8514215942160073, 0.498847153572056, -0.5117595526567502,
-0.1541269987967291, 0.21199829846035145, 0.7834143496676975, -0.16460155429334833,
0.8166857502105551, -0.662728145444706, -0.9440223497598601, 0.34 ... -0.3857042044709651}}, {TemporalData`DateSpecification[{2014, 2, 2, 0, 0, 0.},
{2014, 6, 1, 0, 0, 0}, {1, "Day"}]}, 1, {"Continuous", 1}, {"Discrete", 1}, 1,
{ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}}}, True, 10.1];BinCounts[ts, .1]BinCounts[ts["Values"], .1]%% == %Data with Quantities (3)
data = {Quantity[-1.541, "Grams"], Quantity[-0.849, "Grams"], Quantity[-0.477, "Grams"], Quantity[-0.133, "Grams"], Quantity[-0.11, "Grams"], Quantity[-0.054, "Grams"], Quantity[0.226, "Grams"], Quantity[0.259, "Grams"], Quantity[1.4, "Grams"], Quantity[1.661, "Grams"]};BinCounts[data]BinCounts[data, Quantity[.5, "Grams"]]Bins of fixed width, minimum and maximum:
BinCounts[data, {Quantity[-1, "Grams"], Quantity[1, "Grams"], Quantity[.1, "Grams"]}]BinCounts[data, {{Quantity[-2, "Grams"], Quantity[0, "Grams"], Quantity[2, "Grams"]}}]mat = RandomReal[10, {3, 3}];
mat[[All, 1]] *= Quantity["Meters"];
mat[[All, 3]] *= Quantity["Seconds"];The units are compatible for each dimension:
mat//MatrixFormSpecify bin width for each dimension:
BinCounts[mat, Quantity[2, "Meters"], 3, Quantity[1, "Seconds"]]Normal[%]QuantityArray can be used just like arrays:
data = QuantityArray[RandomReal[1, 6], "Pounds"]BinCounts[data]Specify bin width in compatible units:
BinCounts[data, Quantity[.2, "Kilograms"]]Applications (1)
Properties & Relations (1)
The results from BinCounts are equivalent to the lengths of BinLists:
data = RandomReal[{-5, 5}, 1000];BinCounts[data]Map[Length, BinLists[data]]data2 = RandomReal[{-5, 5}, {1000, 2}];BinCounts[data2] === Map[Length, BinLists[data2], {2}]Possible Issues (4)
Binning intervals are closed on the left:
BinCounts[{1, 2, 3, 4, 5}]BinCounts[{1 - $MachineEpsilon, 2, 3, 4, 5}]For data involving quantities, the bin specification must be given in compatible units:
data = Quantity[RandomReal[1, 20], "Meters"];BinCounts[data, .2]BinCounts[data, Quantity[20, "Centimeters"]]Matrix data must have unit-compatible columns:
mat = {{Quantity[1.03, "Meters"], Quantity[1.65, "Meters"], Quantity[1.49, "Meters"]}, {4.013, 3.40, 2.5}, {Quantity[3.9, "Seconds"], Quantity[1.88, "Seconds"], Quantity[3.52, "Seconds"]}};BinCounts[mat]BinCounts[Transpose@mat]The bins obtained using the specification {min,max,dx} may not include all data points:
data = Range[1, 50];res1 = BinCounts[data, {Min[data], Max[data], 10}]{Length[data], Total[res1]}Specify the min and max so all data points are included in the bin covering:
dx = 10;
min = 1;
max = min + dx * Ceiling[Length[data] / dx];res2 = BinCounts[data, {min, max, dx}]Total[res2]Alternatively, use the width-only bin specification dx:
res3 = BinCounts[data, 10]Total[res3]See Also
BinLists Tally Count PDF Floor Ceiling FindDivisions ImageLevels
Text
Wolfram Research (2007), BinCounts, Wolfram Language function, https://reference.wolfram.com/language/ref/BinCounts.html (updated 2024).
CMS
Wolfram Language. 2007. "BinCounts." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/BinCounts.html.
APA
Wolfram Language. (2007). BinCounts. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/BinCounts.html
BibTeX
@misc{reference.wolfram_2026_bincounts, author="Wolfram Research", title="{BinCounts}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/BinCounts.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_bincounts, organization={Wolfram Research}, title={BinCounts}, year={2024}, url={https://reference.wolfram.com/language/ref/BinCounts.html}, note=[Accessed: 13-June-2026]}