From fa05c1786fbae6c8df90d31187309b78eeb2502e Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Tue, 15 Sep 2026 21:09:58 +0400 Subject: [PATCH 1/3] Fix empty array edge cases in coxphfit --- inst/Regression/coxphfit.m | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/inst/Regression/coxphfit.m b/inst/Regression/coxphfit.m index 555ab16f2..8b6d14b6d 100644 --- a/inst/Regression/coxphfit.m +++ b/inst/Regression/coxphfit.m @@ -162,11 +162,12 @@ endif ## --- X and T ----------------------------------------------------------- - if (! (isnumeric (X) && isreal (X) && ismatrix (X) && ! isempty (X))) + if (! (isnumeric (X) && isreal (X) && ismatrix (X))) error ("coxphfit: X must be a real numeric matrix."); endif - if (! (isnumeric (T) && isreal (T) && ! isempty (T))) - error ("coxphfit: T must be a real numeric vector."); + if (! (isnumeric (T) && isreal (T) + && (isvector (T) || (ismatrix (T) && columns (T) == 2)))) + error ("coxphfit: T must be a vector or 2-column matrix of real numbers."); endif ## T is either a vector of event times or, in the counting process form, an ## N-by-2 matrix whose rows give a (start, stop] interval of exposure. @@ -291,7 +292,11 @@ n = numel (T); endif if (n == 0) - error ("coxphfit: no complete observations remain after removing NaNs."); + if (isempty (X) || isempty (T)) + error ("coxphfit: x and t must contain at least one observation."); + else + error ("coxphfit: no complete observations remain after removing NaNs."); + endif endif ## A column with no variation carries no information. Globally, that is a @@ -834,7 +839,7 @@ %!error ... %! coxphfit (X, [T, T]) -%!error ... +%!error ... %! coxphfit (X, ones (10, 3)) ## Stratified fits, against R2024a @@ -947,3 +952,9 @@ ## A constant column is reported, not silently absorbed %!warning ... %! coxphfit ([X, ones(10,1)], T); + +## Edge cases with empty arrays +%!error ... +%! coxphfit ([], []) +%!error ... +%! coxphfit (zeros(0,3), zeros(0,1)) From 21d39fc4c6d26398b73803089589259714cfa733 Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Wed, 16 Sep 2026 06:30:47 +0400 Subject: [PATCH 2/3] Fix case in coxphfit error and format, revert T checks --- inst/Regression/coxphfit.m | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/inst/Regression/coxphfit.m b/inst/Regression/coxphfit.m index 8b6d14b6d..1fd416cd1 100644 --- a/inst/Regression/coxphfit.m +++ b/inst/Regression/coxphfit.m @@ -165,9 +165,8 @@ if (! (isnumeric (X) && isreal (X) && ismatrix (X))) error ("coxphfit: X must be a real numeric matrix."); endif - if (! (isnumeric (T) && isreal (T) - && (isvector (T) || (ismatrix (T) && columns (T) == 2)))) - error ("coxphfit: T must be a vector or 2-column matrix of real numbers."); + if (! (isnumeric (T) && isreal (T))) + error ("coxphfit: T must be a real numeric vector."); endif ## T is either a vector of event times or, in the counting process form, an ## N-by-2 matrix whose rows give a (start, stop] interval of exposure. @@ -178,7 +177,7 @@ error (strcat ("coxphfit: each row of T must give a (start, stop]", ... " interval with start strictly less than stop.")); endif - elseif (isvector (T)) + elseif (isvector (T) || isempty (T)) T = T(:); Tstart = -Inf (numel (T), 1); else @@ -189,6 +188,9 @@ if (rows (X) != n) error ("coxphfit: T must have one element for each row of X."); endif + if (n == 0) + error ("coxphfit: X and T must contain at least one observation."); + endif p = columns (X); ## --- name/value pairs -------------------------------------------------- @@ -292,11 +294,7 @@ n = numel (T); endif if (n == 0) - if (isempty (X) || isempty (T)) - error ("coxphfit: x and t must contain at least one observation."); - else - error ("coxphfit: no complete observations remain after removing NaNs."); - endif + error ("coxphfit: no complete observations remain after removing NaNs."); endif ## A column with no variation carries no information. Globally, that is a @@ -839,7 +837,7 @@ %!error ... %! coxphfit (X, [T, T]) -%!error ... +%!error ... %! coxphfit (X, ones (10, 3)) ## Stratified fits, against R2024a @@ -954,7 +952,7 @@ %! coxphfit ([X, ones(10,1)], T); ## Edge cases with empty arrays -%!error ... +%!error ... %! coxphfit ([], []) -%!error ... -%! coxphfit (zeros(0,3), zeros(0,1)) +%!error ... +%! coxphfit (zeros (0, 3), zeros (0, 1)) From 5f21777590407550f0b1dbc7e6fb2477412dd7b7 Mon Sep 17 00:00:00 2001 From: Sonu0305 Date: Wed, 16 Sep 2026 06:33:09 +0400 Subject: [PATCH 3/3] Fix formatting --- inst/Regression/coxphfit.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/inst/Regression/coxphfit.m b/inst/Regression/coxphfit.m index 1fd416cd1..9e7cd8064 100644 --- a/inst/Regression/coxphfit.m +++ b/inst/Regression/coxphfit.m @@ -188,9 +188,11 @@ if (rows (X) != n) error ("coxphfit: T must have one element for each row of X."); endif + if (n == 0) error ("coxphfit: X and T must contain at least one observation."); endif + p = columns (X); ## --- name/value pairs --------------------------------------------------