 # Sum rows of a matrix to create new array

Is there a way to sum all elements in a row of a matrix and output a new single column array?

For ex. if I have a matrix:

1 0 1 1
0 0 0 0
0 1 1 0

I want to sum each row to output a new array:

3
0
2

And then I want to use the maximum() function to give me the max value in the array = 3

This is a pretty simple question… The documentation for `sum` explicitly answers it.

``````  Sum elements of an array over the given dimensions.

Examples
≡≡≡≡≡≡≡≡≡≡

julia> A = [1 2; 3 4]
2×2 Array{Int64,2}:
1  2
3  4

julia> sum(A, 1)
1×2 Array{Int64,2}:
4  6

julia> sum(A, 2)
2×1 Array{Int64,2}:
3
7
``````
6 Likes

In Julia 0.7,

``````using Random
srand(0)
obj = rand(4, 4)
maximum(sum(obj, dims = 2))
``````
2 Likes

Please note that this does not work in Julia 1.0

``````**julia&gt;** A = [1 2; 3 4]

2×2 Array{Int64,2}:
1 2
3 4

**julia&gt;** sum(A, 1)
**ERROR:** MethodError: objects of type Array{Int64,2} are not callable
Use square brackets [] for indexing an Array.

Stacktrace:
 **mapreduce_first(** ::Array{Int64,2}, ::Function, ::Int64 **)** at **./reduce.jl:295**
 **mapreduce(** ::Array{Int64,2}, ::Function, ::Int64 **)** at **./reduce.jl:322**
 **sum(** ::Array{Int64,2}, ::Int64 **)** at **./reduce.jl:397**
 top-level scope at **none:0**
``````

But this does

``````**julia&gt;** sum(A, dims=1)

1×2 Array{Int64,2}:
4 6``````
4 Likes

I remember this has been changed since version 0.7, but TBH, I don’t know why this was decided? Why Julia sometimes choose verbosity over brevity when there is no real ambiguity. When I read `sum(A,1)` what else should I assume? Nothing other than summing array elements over the first dimension. For those who used Fortran, MATLAB, or even Python (Numpy) for years, making `dims=1` a mandatory argument is really weird.

Brevity is crucial in dynamic languages with a REPL like Julia. Your’e trying to make the clear thing clearer, even if it’s not so clear to a beginner (someone who didn’t use a programming language before), they will learn it once and write it a million times in the REPL. It could’ve been made optional at most like in other languages. Also, why `dims` and not just `dim` as in Fortran? Only rare cases will include nD arrays, n > 2. Yes, clarity is a good thing, but also choosing a sensible default is very important especially for dynamic coding in the REPL.

PS: I can’t find the issue number for the discussion about this change, I appreciate if someone can provide it here.

I think it is this issue #25989. On the question on why `dims` and not `dim`, you have answered your own question I guess as `sum(rand(3,3,3), dims = (1,2))` works just as well.

There actually was an ambiguity in the case of `sum(x, y)`. It’s not clear if `x` is a callable function to be applied to the elements of `y` or if `x` is the data and `y` is the dimension. If you were to try to use a function-like-object there, it’d fail on 0.6 but work on 1.0.

2 Likes

This solution seems to be depreciated, when I try it i get the following error :

``````ERROR: MethodError: objects of type Array{Int64,2} are not callable
Use square brackets [] for indexing an Array
``````
1 Like

Have you by any chance called an array `sum`? If so, that definition is shadowing the function.

This is because the dimension is now a keyword argument. `sum(A, 2)` will throw that error but `sum(A, dims=2)` works.