From d88a8dab2719650823b2f360c249a3edd25d0869 Mon Sep 17 00:00:00 2001 From: Aaryan Johri <82aaryan@gmail.com@example.com> Date: Tue, 13 Jan 2026 16:02:59 +0530 Subject: [PATCH] Fix #341: Added support for string and categorical arrays in confusionmat --- inst/confusionmat.m | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/inst/confusionmat.m b/inst/confusionmat.m index d75fe1251..fce045b0b 100644 --- a/inst/confusionmat.m +++ b/inst/confusionmat.m @@ -191,11 +191,26 @@ C(row_index, col_index)++; endfor - elseif (isstring (y_true)) - ## string array - ## FIXME: not implemented yet + // MY FIX STARTS + elseif (isstring (y_true) || iscategorical (y_true)) + ## string or categorical array - error ("confusionmat: string array not implemented yet"); + if (! exist ("unique_tokens", "var")) + unique_tokens = union (y_true, y_pred); + endif + + C_size = length (unique_tokens); + C = zeros (C_size); + + for i = 1:length (y_true) + row_index = find (unique_tokens == y_true(i)); + col_index = find (unique_tokens == y_pred(i)); + + if (! isempty (row_index) && ! isempty (col_index)) + C(row_index, col_index)++; + endif + endfor + // FIX COMPLETED else error ("confusionmat: invalid data type"); endif