Skip to content

Fix nanmean input validation - #517

Open
sahilphad07-sudo wants to merge 2 commits into
gnu-octave:mainfrom
sahilphad07-sudo:fix-nanmean-validation
Open

sahilphad07-sudo wants to merge 2 commits into
gnu-octave:mainfrom
sahilphad07-sudo:fix-nanmean-validation

Conversation

@sahilphad07-sudo

@sahilphad07-sudo sahilphad07-sudo commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

nanmean did not correctly handle logical inputs and did not validate some invalid dimension arguments. This change adds logical input support and validates non-finite, duplicate, and empty dimension arguments.

Before

octave:1> nanmean (logical ([0 1 1]))
error: nanmean: X must be numeric.

octave:2> nanmean (ones (3), [])
ans =

   1   1   1
   1   1   1
   1   1   1

For Inf and duplicate dimension cases, the previous implementation did not perform the required validation.

After

octave:1> nanmean (logical ([0 1 1]))
ans = 0.6667

octave:2> nanmean (ones (3), Inf)
error: nanmean: DIM must be a positive integer.

octave:3> nanmean (ones (3), [1 Inf])
error: nanmean: VECDIM must be a vector of positive integers.

octave:4> nanmean (ones (3), [1 1])
error: nanmean: VECDIM must be a vector of positive integers.

octave:5> nanmean (ones (3), [])
error: nanmean: DIM must be a positive integer.

MATLAB Behavior

>> nanmean(logical([0 1 1]))

ans =

    0.6667

>> nanmean(ones(3), Inf)
Error using sum
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.

>> nanmean(ones(3), [1 Inf])
Error using sum
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.

>> nanmean(ones(3), [1 1])
Error using sum
Elements of a dimension vector must be unique.

>> nanmean(ones(3), [])
Error using mean
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.

Regression Tests Added

  • Logical input.
  • Non-finite scalar dimension.
  • Non-finite dimension in a vector.
  • Duplicate dimensions.
  • Empty dimension argument.

Test Results

  • 26 out of 26 nanmean BISTs pass.

This branch has not been deployed

No deployments
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.

1 participant