Sum dimensions of matrix


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.


Try this:

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

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

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

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

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

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.


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

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

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.


This does work:

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.


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> 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> sum(A[12, 35, v], dims=3)
5-element Array{Float64,1}: …

Shouldn’t I be getting rather a single element?


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:

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

This works perfectly fine with your example.


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()?


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.


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!