# Sum rows of a matrix to create new array

**URL:** <https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470>\
**Category:** New to Julia\
**Tags:** arrays, matrices\
**Created:** [March 2, 2018, 10:45pm UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470 "2018-03-02T22:45:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhislop](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dhislop](https://discourse.julialang.org/u/dhislop)\
**Post date:** [March 2, 2018, 10:45pm UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/1 "2018-03-02T22:45:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 2, 2018, 10:49pm UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/2 "2018-03-02T22:49:32Z")

</div>

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

```julia
  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

```

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [March 3, 2018, 8:45pm UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/3 "2018-03-03T20:45:55Z")

</div>

In Julia 0.7,

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

```

---

<div class="post-metadata">

**Author:** ![peter.wolf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter.wolf/32/6018_2.png) [@peter.wolf](https://discourse.julialang.org/u/peter.wolf)\
**Post date:** [December 7, 2018, 3:52pm UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/4 "2018-12-07T15:52:09Z")

</div>

> [@tbeason](#):
>
> sum(A, 1)

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:
  [1] **mapreduce_first(** ::Array{Int64,2}, ::Function, ::Int64 **)** at **./reduce.jl:295**
  [2] **mapreduce(** ::Array{Int64,2}, ::Function, ::Int64 **)** at **./reduce.jl:322**
  [3] **sum(** ::Array{Int64,2}, ::Int64 **)** at **./reduce.jl:397**
  [4] top-level scope at **none:0**

```

But this does

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

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

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [December 7, 2018, 7:17pm UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/5 "2018-12-07T19:17:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [December 7, 2018, 8:52pm UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/6 "2018-12-07T20:52:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [December 7, 2018, 9:08pm UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/7 "2018-12-07T21:08:07Z")

</div>

> [@Seif\_Shebl](#):
>
> Why Julia sometimes choose verbosity over brevity when there is no real ambiguity

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.

---

<div class="post-metadata">

**Author:** ![Johncowk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johncowk/32/8032_2.png) [@Johncowk](https://discourse.julialang.org/u/Johncowk)\
**Post date:** [November 29, 2019, 11:40am UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/8 "2019-11-29T11:40:55Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 29, 2019, 11:55am UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/9 "2019-11-29T11:55:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)\
**Post date:** [October 13, 2021, 7:28am UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/10 "2021-10-13T07:28:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Xavier\_Gonzalez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xavier_gonzalez/32/36033_2.png) [@Xavier\_Gonzalez](https://discourse.julialang.org/u/Xavier_Gonzalez)\
**Post date:** [October 24, 2022, 3:45pm UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/11 "2022-10-24T15:45:45Z")

</div>

In

```julia
Julia Version 1.8.0
Commit 5544a0fab76 (2022-08-17 13:38 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.3.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, apple-m1)
  Threads: 1 on 8 virtual cores

```

Your example leads to the following behavior:

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

julia> sum(A, 1)
ERROR: MethodError: objects of type Matrix{Int64} are not callable
Use square brackets [] for indexing an Array.
Stacktrace:
 [1] mapreduce_first(f::Matrix{Int64}, op::Function, x::Int64)
   @ Base ./reduce.jl:411
 [2] mapreduce(f::Matrix{Int64}, op::Function, a::Int64)
   @ Base ./reduce.jl:438
 [3] sum(f::Matrix{Int64}, a::Int64; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Base ./reduce.jl:520
 [4] sum(f::Matrix{Int64}, a::Int64)
   @ Base ./reduce.jl:520
 [5] top-level scope
   @ REPL[32]:1

```

What changed and how to we get back to the desired behavior?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [October 24, 2022, 3:56pm UTC](https://discourse.julialang.org/t/sum-rows-of-a-matrix-to-create-new-array/9470/12 "2022-10-24T15:56:26Z")

</div>

Again, the documentation for `sum` explicitly answers this question (even almost 4 years later).

`sum(A; dims=1)` is now the proper syntax.
