help-octave
[Top][All Lists]

## Re: sum() behaving erratically?

 From: Bart Vandewoestyne Subject: Re: sum() behaving erratically? Date: Thu, 7 Oct 2004 02:58:33 -0500 User-agent: Mutt/1.3.28i

```on 10/6/04 4:14 PM, address@hidden at address@hidden wrote:

> I'm a bit embarrassed to ask this, but here goes:
>
> If I let a=[1,2;3,4]; and then ask for sum(a), sum(a,1) and sum(a,2), I
>
> 4 6     4 6     3
>                 7
>
> respectively. Now I let a=[1,2]; and do the same. I now get
>
> 3       1 2     3
>
> indicating that in the second case sum(a) != sum(a,1), in direct violation
> of the documentation in the manual. Is this supposed to be like this and
> didn't I understand things, or is it a bug? (Octave is 2.1.57.)

For what it's worth, here is how the other leading brand behaves:

>> version

ans =

6.5.0.180913a (R13

>> a=[1,2;3,4];
>> sum(a)

ans =

4     6

>> sum(a,1)

ans =

4     6

>> sum(a,2)

ans =

3
7

and

>> a=[1,2];
>> sum(a)

ans =

3

>> sum(a,1)

ans =

1     2

>> sum(a,2)

ans =

3

So the behaviour is the same as in the other leading brand.  I also came across
this 'problem' while doing some implementation work, but actually i think the
behaviour as it is is quite intuitive, since when a is a vector, then saying
sum(a) is just saying that you want the sum of all elements of a.  Wether a is
a row or a column vector doesn't matter than.

The best thing to do is if you really have a situation where you're summing
over colums or rows, is *always* put the 1 or 2 as an extra argument of sum to
indictate if you really want to sum over columns or rows.

Regards,
Bart

--
!!!!!!!!!!!!!!!!!!! email change !!!!!!!!!!!!!!!!!!!!!!!
My email address is now Bart.Vandewoestyne AT telenet.be
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```