bugGNU Octave - Bugs: bug #58089, [Feature Request] sum along...


bug #58089: [Feature Request] sum along multiple dimensions

Submitter:  Guillaume <gyom>
Submitted:  Tue 31 Mar 2020 05:00:43 PM UTC
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Feature Request
Status:  Confirmed Assigned to:  None
Originator Name:  Guillaume Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup

Jump to the original submission

Wed 01 Mar 2023 06:38:48 PM UTC, comment #8: 

that should have said patch #10314

Nicholas Jankowski <nrjank>
Group Member
Wed 01 Mar 2023 03:13:40 PM UTC, comment #7: 

FYI regarding the list in commet #1, patch #10301 has been pushed to default that includes vecdim handling in mean, median, std, var (limited to those implemented in m-files).  I don't think much progress has been made on vector dimensions for any compiled functions.

Nicholas Jankowski <nrjank>
Group Member
Fri 10 Apr 2020 02:03:48 AM UTC, comment #6: 

You can use the octave_value function is_scalar_type() to determine if the argument is a scalar.  This is faster than checking whether the number of elements is 1.

Rik <rik5>
Group administrator
Thu 09 Apr 2020 10:14:04 PM UTC, comment #5: 

diff -r 7ac8b3188b27 libinterp/corefcn/data.cc
--- a/libinterp/corefcn/data.cc Wed Apr 08 16:03:51 2020 -0400
+++ b/libinterp/corefcn/data.cc Thu Apr 09 23:09:42 2020 +0100
@@ -2953,6 +2953,8 @@
   int dim = -1;
   if (nargin == 2)
+      if (args(1).length () != 1)
+        error ("sum: dimension must be a scalar");
       dim = args(1).int_value () - 1;
       if (dim < 0)
         error ("sum: invalid dimension DIM = %d", dim + 1);

but I guess one could also check whether the input is an integer and apply a similar fix to all of the functions listed in comment #2. I can try and have a look.

Guillaume <gyom>
Thu 09 Apr 2020 09:39:55 PM UTC, comment #4: 

I think improved input validation (! isscalar (dim)) would be a fine idea until this feature is actually implemented.

Rik <rik5>
Group administrator
Thu 09 Apr 2020 09:28:44 PM UTC, comment #3: 

Thanks Rik. Until this is implemented in Octave, do you think it would be possible to raise an error if ! isscalar (dim) ?

Guillaume <gyom>
Fri 03 Apr 2020 03:31:15 PM UTC, comment #2: 

Changing category to Feature Request.  This is something that Matlab has added relatively recently as you point out.

If you really want to use this functionality in your code in the meantime it wouldn't be that hard to add an m-file to override sum().  Below is an example which is also attached as sum.m

function s = sum (x, dim)

  if (nargin < 1 || nargin > 2)
    print_usage ("sum");

  if (nargin == 1)
    s = builtin ("sum", x);
    if (! isnumeric (dim) || ! isvector (dim))
      error ("sum: DIM must be a numeric scalar or vector");
    s = builtin ("sum", x, dim(1));
    for i = 2:numel (dim)
      s = builtin ("sum", s, dim(i));


(file #48747)

Rik <rik5>
Group administrator
Tue 31 Mar 2020 07:51:27 PM UTC, comment #1: 

This syntax was recently introduced in R2018b:
and applies to "all, any, bounds, max, min, mean, median, mode, prod, std, and var".

Guillaume <gyom>
Tue 31 Mar 2020 05:00:43 PM UTC, original submission:  

In Matlab, the dim parameter for sum can be a vector and not just a scalar, in order to sum across multiple dimensions:

Octave only implements the scalar version and silently ignores the other dimensions if they were provided. See this line in libinterp/corefcn/data.cc:

dim = args(1).int_value () - 1;

As a consequence, Octave does this:

octave:7> sum(ones(2,3),[1 2])
ans =
   2   2   2
octave:8> sum(ones(2,3),[1 Inf 2 NaN 0 -eps])
ans =
   2   2   2

while Matlab does:

>> sum(ones(2,3),[1 2])
ans =
>> sum(ones(2,3),[1 Inf 2 NaN 0 -eps])
Error using sum
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.

Guillaume <gyom>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

Attached Files
file #48747:  sum.m added by rik5 (397B - text/x-matlab)


Depends on the following items: None found

   bug dependencies.


Carbon-Copy List
  • -email is unavailable- added by nrjank (Posted a comment)
  • -email is unavailable- added by rik5 (Updated the item)
  • -email is unavailable- added by gyom (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.


    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-01-24 nrjank Dependencies- bugs #63698 is dependent
    2020-04-03 rik5 Attached File- Added sum.m, #48747
        Item GroupIncorrect Result Feature Request
        StatusNone Confirmed
        Summarysum along multiple dimensions [Feature Request] sum along multiple dimensions

    Back to the top

    Powered by Savane 3.13-4448.
    Corresponding source code