Skip to content

Fix combnk MATLAB behavior - #504

Closed
sahilphad07-sudo wants to merge 2 commits into
gnu-octave:mainfrom
sahilphad07-sudo:fix-combnk-matlab-behavior
Closed

sahilphad07-sudo wants to merge 2 commits into
gnu-octave:mainfrom
sahilphad07-sudo:fix-combnk-matlab-behavior

Conversation

@sahilphad07-sudo

Copy link
Copy Markdown
Contributor

combnk did not match MATLAB behavior for non-positive values of k.

For k = 0, Octave returned a 0x0 empty result instead of MATLAB's 1x0 empty row vector. Negative values of k were also rejected, while MATLAB returns a 1x0 empty row vector.

This change updates the handling of non-positive k values to match MATLAB behavior.

Before

octave:> combnk(1:3, 0)
ans =

[](0x0)

octave:> combnk(1:3, -1)
error: combnk: second input argument must be a non-negative integer

After

octave:> combnk(1:3, 0)
ans =

[](1x0)

octave:> combnk(1:3, -1)
ans =

[](1x0)

MATLAB Behavior

>> combnk(1:3, 0)
ans =

     1×0 empty double row vector

>> combnk(1:3, -1)
ans =

     1×0 empty double row vector

Regression Tests Added

  • k = 0
  • Negative k

Test Results

All existing and new combnk BISTs pass:

PASSES 6 out of 6 tests.

@sahilphad07-sudo sahilphad07-sudo changed the title Fix combnk matlab behavior Fix combnk MATLAB behavior Sep 20, 2026
@pr0m1th3as

Copy link
Copy Markdown
Member

Thank you, but I am closing this without merging.

The k = 0 part is right: MATLAB returns a 1-by-0 result of the input's class, one empty combination, and I will apply that myself. A negative k has no meaning, though, and MATLAB returning 1x0 for it, even for k = -2.5, is not behaviour worth copying, so combnk keeps refusing it. As written, the patch also accepts negative values while its error message still asks for a non-negative integer.

@pr0m1th3as pr0m1th3as closed this Sep 24, 2026
pr0m1th3as added a commit that referenced this pull request Sep 24, 2026
  * inst/Data_Manipulation/combnk.m: return a 1-by-0 array of the class
  of DATA for K = 0; describe K in the help text; add tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants