confusionmat — Compute a confusion matrix from true and predicted class labels.
confusionmat(group,grouphat) compares true class labels in group with predicted labels in grouphat and returns a matrix whose rows are true classes and columns are predicted classes.
Syntax
C = confusionmat(group, grouphat)
C = confusionmat(group, grouphat, Name, Value)
[C,order] = confusionmat(___)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
group | Any | Yes | — | True class labels. |
grouphat | Any | Yes | — | Predicted class labels. |
nameValuePairs | Any | Variadic | — | Name-value options such as Order. |
Returns
| Name | Type | Description |
|---|---|---|
C | NumericArray | Confusion matrix with true labels in rows and predicted labels in columns. |
order | Any | Class labels corresponding to rows and columns of C. |
Returned values from confusionmat depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:confusionmat:InvalidArgument | Label vectors, dimensions, order labels, or name-value options are malformed. | confusionmat: invalid argument |
RunMat:confusionmat:Internal | RunMat cannot allocate or construct confusion matrix outputs. | confusionmat: internal error |
How confusionmat works
groupandgrouphatmust contain the same number of labels.- Numeric, logical, string, character-row, categorical, and cell-string labels are supported. Text label families can be mixed between string, char, cell-string, and categorical inputs.
- Without an explicit order, numeric labels are sorted ascending, text labels follow first appearance across
groupthengrouphat, categorical labels follow category order for observed categories, and logical labels are ordered false then true. confusionmat(group,grouphat,'Order',order)uses the supplied class order, requires every nonmissing label ingroupandgrouphatto appear inorder, and includes zero rows or columns for extra order labels that do not appear in the data.- Rows with missing true or predicted labels are omitted. Numeric
NaN, empty text, and<missing>text are treated as missing labels. [C,order] = confusionmat(...)returns the class labels corresponding to the rows and columns ofC, preserving the label family of the inferred or explicit order.
Examples
Build a numeric confusion matrix
truth = [1; 1; 2; 2];
pred = [1; 2; 2; 1];
[C,order] = confusionmat(truth, pred)Expected output:
C is [1 1; 1 1] and order is [1; 2].Use an explicit class order
C = confusionmat(["cat";"dog";"dog"], ["dog";"dog";"cat"], 'Order', ["dog";"cat"])Expected output:
Rows and columns are ordered as dog, then cat.Using confusionmat with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how confusionmat changes the result.
Run a small confusionmat example, explain the result, then change one input and compare the output.
FAQ
Does confusionmat normalize counts?⌄
No. It returns raw counts. Normalize rows or columns explicitly after computing C.
How are labels not present in an explicit Order handled?⌄
When Order is supplied, it must contain every nonmissing true and predicted label. Extra order labels are allowed and produce zero rows or columns.
Related Stats functions
Ml
bayesopt · classify · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Summary
binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how confusionmat is executed, line by line, in Rust.
- View the source for confusionmat in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.