0

I am using matlab R2012a to compute the column sum of a matrix using a sum() function. But I am getting either an incorrect answer or an error as show below:

>> A = ones(4,8)

A =

     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1

>> sum (A)

ans =

     9     9     9     9     9     9     9     9
     9     9     9     9     9     9     9     9
     9     9     9     9     9     9     9     9
     9     9     9     9     9     9     9     9

sum (A,2) Index exceeds matrix dimensions.

I have also tried the sum() function on matlab 2014 version and it gives a correct output there. So is this problem only related to 2012 version of matlab or am I missing something obvious?

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
user1228310
  • 111
  • 1
  • 1
  • 4
  • 1
    On your R2012a, what is the result of `which sum`? My first guess is that another function has the same name, and you're running that instead of the built-in version of sum. – Matthew Gunn Nov 23 '15 at 06:19
  • There are documentation for all previous versions of Matlab here: http://www.mathworks.com/help/doc-archives.html (but you need to be registered). So you can find `sum` function for 2012 – Mikhail_Sam Nov 23 '15 at 06:48
  • 3
    Besides what @MatthewGunn mentioned, it could also be possible that you have a variable called `sum` on the workspace. Then `sum(A)` will index that variable, producing an output the same size as `A`. – mikkola Nov 23 '15 at 06:52
  • 2
    @mikkola Yeah, he probably has a variable `sum` with the value 9. – Matthew Gunn Nov 23 '15 at 06:58

0 Answers0