I want to do a sum over one dimension in an array. That's easy. For an array 9x100x100
我想在数组中对一个维度进行求和。这很容易。对于阵列9x100x100
sum(a,1)
However what is then left is a array with dimension 1x100x100. And now i want to get rid of the first dimension, since there is only one element left. So my solution is just:
然而,剩下的是一个尺寸为1x100x100的数组。现在我想摆脱第一个维度,因为只剩下一个元素。所以我的解决方案只是:
reshape(summed_array, 100,100)
in oder to get the 100x100 array, which i wanted. However this does not feel very clean. Is there a better way of achieving this?
在奥得河,以获得我想要的100x100阵列。然而,这感觉不是很干净。有没有更好的方法来实现这一目标?
1 个解决方案
#1
10
You're looking for squeeze
:
你正在寻找挤压:
squeeze(A, dims)
Remove the dimensions specified by dims from array
A
. Elements ofdims
must be unique and within the range1:ndims(A)
.从阵列A中删除dims指定的尺寸.dims的元素必须是唯一的,并且在1:ndims(A)的范围内。
Example
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
#1
10
You're looking for squeeze
:
你正在寻找挤压:
squeeze(A, dims)
Remove the dimensions specified by dims from array
A
. Elements ofdims
must be unique and within the range1:ndims(A)
.从阵列A中删除dims指定的尺寸.dims的元素必须是唯一的,并且在1:ndims(A)的范围内。
Example
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