使用例如滑动窗口=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/