histcounts2 — Count paired observations into two-dimensional histogram bins.
histcounts2(X, Y) bins paired observations from X and Y into a rectangular two-dimensional histogram. Each bin counts the number of pairs whose X component lies in a horizontal interval and whose Y component lies in a vertical interval, mirroring MathWorks MATLAB behaviour.
How does the histcounts2 function behave in MATLAB / RunMat?
histcounts2(X, Y)flattens both inputs column-major, ensuring they contain the same number of elements, and fills anumel(Xedges) - 1bynumel(Yedges) - 1matrix of bin counts.- Bins are half-open on the right except for the last column and row, which include their upper edges so the maximum finite values are counted.
- Optional arguments let you specify bin counts, explicit edges, bin limits, bin widths, and automatic binning heuristics independently for the
XandYaxes. - Name/value pairs such as
'NumBins','XBinEdges','YBinEdges','XBinWidth','YBinWidth','BinMethod', and'Normalization'follow MATLAB precedence and validation rules. - Pairs containing
NaNin either coordinate are ignored. Infinite values participate when the chosen edges include them.
GPU behavior
When either input is a gpuArray, RunMat gathers the samples back to host memory, performs the reference CPU implementation, and returns dense CPU tensors for the histogram and edges. The acceleration layer exposes a histcounts2 provider hook; once kernels land, the runtime will automatically keep residency on the GPU and skip gathering. The builtin is registered as a sink, so fusion plans flush GPU residency before histogramming and the current implementation always yields host-resident outputs.
GPU residency
As with other RunMat histogram routines, you do not need to call gpuArray explicitly just to obtain GPU execution. Once providers implement the histcounts2 hook, the fusion planner will keep residency on the device automatically. Until then, the builtin gathers data to the host and returns CPU tensors even when inputs originate on the GPU, matching MATLAB semantics after an explicit gather.
Examples of using histcounts2 in MATLAB / RunMat
Counting paired values with explicit edges
X = [0.5 1.5 2.5 3.5];
Y = [0.2 0.9 1.4 2.8];
[N, Xedges, Yedges] = histcounts2(X, Y, [0 1 2 3 4], [0 1 2 3])Expected output:
N = [1 0 0; 1 0 0; 0 1 0; 0 0 1];
Xedges = [0 1 2 3 4];
Yedges = [0 1 2 3]Specifying separate bin counts for each axis
X = [1 2 3 4];
Y = [1 2 3 4];
N = histcounts2(X, Y, 2, 4)Expected output:
size(N) = [2 4];
sum(N, "all") = 4Using different bin widths for X and Y
X = [1 1.5 2.4 3.7];
Y = [2 2.2 2.9 3.1];
[N, Xedges, Yedges] = histcounts2(X, Y, 'XBinWidth', 1, 'YBinWidth', 0.5)Expected output:
diff(Xedges) = [1 1 1];
diff(Yedges(1:3)) = [0.5 0.5];
sum(N, "all") = 4Normalizing a 2-D histogram to probabilities
X = [0.2 0.4 1.1 1.5];
Y = [0.1 0.8 1.2 1.9];
N = histcounts2(X, Y, [0 1 2], [0 1 2], 'Normalization', 'probability')Expected output:
N = [0.5 0.0; 0.0 0.5]Ignoring NaN values in paired data
X = [1 2 NaN 3];
Y = [2 2 2 NaN];
N = histcounts2(X, Y, [0 1 2 3], [0 1 2 3])Expected output:
sum(N, "all") = 2Histogramming gpuArray inputs without manual gather
Gx = gpuArray([0.5 1.5 2.5]);
Gy = gpuArray([1.0 1.1 2.9]);
[counts, Xedges, Yedges] = histcounts2(Gx, Gy, [0 1 2 3], [0 2 3])Expected output:
isa(counts, 'double') % counts are returned on the CPU
counts = [1 0; 1 0; 0 1]FAQ
Do X and Y need the same size?
Yes. histcounts2 requires both inputs to contain the same number of elements. RunMat mirrors MATLAB by raising an error when the sizes do not match.
How are the bin edges interpreted?
All interior bins are [left, right), while the last row and column are [left, right], so the largest finite values are counted.
What happens to NaN, Inf, or -Inf values?
Pairs containing NaN in either coordinate are ignored. Infinite values participate when the specified edges include them; otherwise, they are excluded just like MATLAB.
Can I mix explicit edges on one axis with automatic binning on the other?
Yes. You can supply 'XBinEdges' while leaving the Y axis to be determined by 'NumBins', 'YBinWidth', or the default heuristics.
Which normalisation modes are supported?
'count', 'probability', 'countdensity', 'pdf', 'cumcount', and 'cdf' are implemented. 'cdf' and 'cumcount' operate in column-major order so the result matches MATLAB's cumulative behaviour.
How do I request integer-aligned bins?
Use 'BinMethod', 'integers' or 'XBinMethod'/'YBinMethod' with the value 'integers'. RunMat ensures the resulting edges align with integer boundaries, respecting any supplied bin limits.
See also
Source & Feedback
- Source code: crates/runmat-runtime/src/builtins/stats/hist/histcounts2.rs
- Found a bug? Open an issue with a minimal reproduction.