How to sum over a measurement in an array when discarding a measurement?

I want to make a sum over one dimension in an array. It's simple. For a 9x100x100 array:

sum(a,1)

However, what remains is an array with a size of 1x100x100. And now I want to get rid of the first dimension, since there is only one element left. So my solution is simple:

reshape(summed_array, 100,100)

In order to get the 100x100 array that I wanted. However, this does not seem very clean. Is there a better way to achieve this?

+5
source share
1 answer

Refresh

@E_net4 , Julia 1.0, dropdims ( !), squeeze.

squeeze:

squeeze(A, dims)

, dims, A dims 1:ndims(A).

julia> a = rand(4,3,2)
4x3x2 Array{Float64,3}:
[:, :, 1] =
 0.333543  0.83446   0.659689
 0.927134  0.885299  0.909313
 0.183557  0.263095  0.741925
 0.744499  0.509219  0.570718

[:, :, 2] =
 0.967247  0.90947   0.715283
 0.659315  0.667984  0.168867
 0.120959  0.842117  0.217277
 0.516499  0.60886   0.616639

julia> b = sum(a, 1)
1x3x2 Array{Float64,3}:
[:, :, 1] =
 2.18873  2.49207  2.88165

[:, :, 2] =
 2.26402  3.02843  1.71807

julia> c = squeeze(b, 1)
3x2 Array{Float64,2}:
 2.18873  2.26402
 2.49207  3.02843
 2.88165  1.71807
+10

Source: https://habr.com/ru/post/1650415/


All Articles