gpt4 book ai didi

arrays - 使用滑动窗口查看 Julia 数组

转载 作者:行者123 更新时间:2023-12-01 23:48:46 25 4
gpt4 key购买 nike

使用例如滑动窗口=2 在数组上创建 View 的最有效方法是什么

假设我们有:

x = collect(1:1:6)
# 1 2 3 4 5 6

我想创建一个这样的 View :

# 1 2
# 2 3
# 3 4
# 4 5
# 5 6

到目前为止,我只找到了这个选项,但不确定它是否是最佳选项:

y = Array{Float32, 2}(undef, nslides, window)
@inbounds for i in 1:window
y[:, i] = @view x[i:end-(window-i)]
end

最佳答案

一个带包的解决方案(嗯,我的包)是这样的:

julia> using Tullio

julia> x = 1:6; window = 2;

julia> @tullio y[r,c] := x[r+c-1] (c in 1:window)
5×2 Matrix{Int64}:
1 2
2 3
3 4
4 5
5 6

关于arrays - 使用滑动窗口查看 Julia 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63769931/

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