gpt4 book ai didi

arrays - 如何在删除遍历维度的同时对数组中的维度求和?

转载 作者:行者123 更新时间:2023-12-04 13:57:57 25 4
gpt4 key购买 nike

我想对数组中的一维求和。这很容易。对于 9x100x100 阵列:

sum(a,1)

然而,剩下的是一个尺寸为 1x100x100 的数组。现在我想摆脱第一个维度,因为只剩下一个元素了。所以我的解决方案是:
reshape(summed_array, 100,100)

为了获得我想要的 100x100 阵列。
不过这感觉不是很干净。有没有更好的方法来实现这一目标?

最佳答案

更新
@E_net4a comment 中指出下面,从 Julia 1.0 开始,你应该使用 dropdims (一个更好的名字!)而不是 squeeze .
原答案
您正在寻找 squeeze :

squeeze(A, dims)

Remove the dimensions specified by dims from array A. Elements of dims must be unique and within the range 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

关于arrays - 如何在删除遍历维度的同时对数组中的维度求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38806584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com