# Sum dimensions of matrix

**URL:** https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098
**Category:** Data
**Tags:** matlab
**Created:** [February 22, 2019, 9:47pm UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098 "2019-02-22T21:47:50Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![nik.ltin](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@nik.ltin](https://discourse.julialang.org/u/nik.ltin)
#### Post date: [February 22, 2019, 9:47pm UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/1 "2019-02-22T21:47:50Z")

</div>

Hi there,

A bit new to Julia so this might be a very basic question: I have a file with a 3 dimensional array (x,y,z), and I’d like to sum certain values from the z dimension to later on pull from and graph. For example, if my array is 30x40x10, and I want to sum values from 1:4 and 6 from the z dimension, I tried `sum(A, [1:4,6])` which didn’t work, and variations of it.

Also, would there be a more general way to write it not specifying the matrix? For example, if I have two matrices A and B with the same dimensions above (but different values in the array) and I want to sum the same positions as above (i.e. 1:4 and 6 of z-dimension), how do I write this “sum” command such that later on in my code I can just specify something like `A[:12, :35, sum], B[:17, :28, sum]`?

Thanks for all help.

---

<div class="post-metadata">

### Author: ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)
#### Post date: [February 22, 2019, 10:08pm UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/2 "2019-02-22T22:08:29Z")

</div>

Try this:

```julia
julia> A = ones(30, 40, 10);

julia> sum(A[:, :, [1:4; 6]])
6000.0

julia> v = [1:4; 6]
5-element Array{Int64,1}:
 1
 2
 3
 4
 6

julia> sum(A[:, :, v])
6000.0

julia> sum(A[12, 35, v])
5.0

```

---

<div class="post-metadata">

### Author: ![nik.ltin](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@nik.ltin](https://discourse.julialang.org/u/nik.ltin)
#### Post date: [February 22, 2019, 10:25pm UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/3 "2019-02-22T22:25:30Z")

</div>

I’m not certain I fully understand this, for the last part of your code  
`julia> sum(A[12, 35, v])`  
does this sum up the values in the 12th, 35th, and “v” dimensions?  
My idea was to _only_ sum up the values in the 1:4,6 of the z-dimension. And at the end, I’d like to plot A and B with their different x and y coordinates as I change them per different specifications, but keep the same z coordinate value that is the sum.

---

<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: [February 22, 2019, 10:27pm UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/4 "2019-02-22T22:27:00Z")

</div>

Then you’d want `sum(A[:, :, [1:4; 6]], dims=3)`:

```julia
julia> sum(A[:, :, [1:4; 6]], dims=3)
30×40×1 Array{Float64,3}:
[:, :, 1] =
...

```

---

<div class="post-metadata">

### Author: ![nik.ltin](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@nik.ltin](https://discourse.julialang.org/u/nik.ltin)
#### Post date: [February 23, 2019, 7:09am UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/6 "2019-02-23T07:09:28Z")

</div>

So I tried this as well, however it shows an error: `BoundsError: attempting to access 30x40x10 Array{Float64,3} at index [12, 35, [1, 2, 3, 4, 6]]`. It’s the same error that’s been showing up since everything else I tried.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 23, 2019, 7:33am UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/7 "2019-02-23T07:33:01Z")

</div>

This _does_ work:

```julia
julia> A = rand(30,40,10);

julia> sum(A[:, :, [1:4; 6]], dims=3)
30×40×1 Array{Float64,3}:
[:, :, 1] =
...

```

so if it fails for you, please provide exact code that reproduces the error, including an example array, `A`.

---

<div class="post-metadata">

### Author: ![nik.ltin](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@nik.ltin](https://discourse.julialang.org/u/nik.ltin)
#### Post date: [February 23, 2019, 8:22am UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/8 "2019-02-23T08:22:48Z")

</div>

Does this work if I specify `julia> sum(A[:, :, [1:4; 6]], dims=3)` if I specify dimensions in x and y coordinates? i.e. my (pseudo-)code is:

```julia
julia> v = [1:4; 6]
julia> sum(A[12, 35, v], dims=3)

```

A is simply a 30x40x10 arrays (in a .mat file) with decimals ranging from 0 to 1.

Update: when I run `julia> sum(A[12, 35, v], dims=3)` on REPL, it shows:

```julia
julia> sum(A[12, 35, v], dims=3)
5-element Array{Float64,1}: …

```

Shouldn’t I be getting rather a single element?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 23, 2019, 8:43am UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/9 "2019-02-23T08:43:22Z")

</div>

There is something wrong with your array `A`. Unless you can provide a reproducible example (code that we can paste and run to reproduce your error), we won’t get any further.

At least copy and run this code on your computer to see what happens:

```julia
A = rand(30,40,10);
sum(A[:, :, [1:4; 6]], dims=3)

```

---

<div class="post-metadata">

### Author: ![nik.ltin](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@nik.ltin](https://discourse.julialang.org/u/nik.ltin)
#### Post date: [February 23, 2019, 8:44am UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/10 "2019-02-23T08:44:25Z")

</div>

This works perfectly fine with your example.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 23, 2019, 8:50am UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/11 "2019-02-23T08:50:30Z")

</div>

So then the problem is with your array. Using your array, `A`, the one that gives the error, what are the outputs of `summary(A)` and `A[:, :, [1:4; 6]]`. Make sure to copy the _entire_ error message.

Also, what is the output of `versioninfo()`?

---

<div class="post-metadata">

### Author: ![fhenneke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fhenneke/32/2211_2.png) [@fhenneke](https://discourse.julialang.org/u/fhenneke)
#### Post date: [February 23, 2019, 9:56am UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/12 "2019-02-23T09:56:46Z")

</div>

`sum(A[12, 35, v])` and `B = sum(A[:, :, v], dims=3); B[12, 35]` might do what you intend.  
`sum(A[12, 35, v], dims=3)` does not sum anything since `A[12, 35, v]` is a vector and has no third dimension.

---

<div class="post-metadata">

### Author: ![nik.ltin](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@nik.ltin](https://discourse.julialang.org/u/nik.ltin)
#### Post date: [February 23, 2019, 2:25pm UTC](https://discourse.julialang.org/t/sum-dimensions-of-matrix/21098/13 "2019-02-23T14:25:20Z")

</div>

I reloaded the dataset and apparently it had some missing values issues. The new dataset works perfectly with `sum(A[12, 35, [1:4; 6]], dims=3)`.  
Should’ve checked this earlier, sorry about time wasted.

Thanks so much for all help!
