1

我的老朋友名为 Pop 的 3d 数组,我想删除列 (d2),当 d1 末尾的值 == 1 时,跨越所有 d3,所以我有以下代码:

Pop[end, :, 1] .!=1

我认为@view在此之前添加会将更改写回 Pop,而不是在内存中产生额外的副本。此代码工作正常

@view(Pop[ :, Pop[end, :, 1] .!=1, :])

但它不会改变名为 Pop 的原始 3d 数组。我可以

Pop = @view(Pop[ :, Pop[end, :, 1] .!=1, :])

但我相信这会在我试图避免的内存中创建另一个副本。我错过了什么简单的语法?谢谢。Ĵ

4

1 回答 1

2

@view不会修改原始数组的大小,它会在其中提供一个“视图”(例如,在您的情况下省略一些列)。我不认为有什么问题

Pop = view(Pop, :, Pop[end, :, 1] .≠ 1, :)

因为 nowPop是旧的 full 的视图Pop,但它的行为类似于数组,因此您可以修改其条目,例如,您可以执行以下操作

julia> using Random # using a fixed seed for reproducibility

julia> Random.seed!(0) ;

julia> Pop = rand(1:5, (2,4,2)) # original Pop
2×4×2 Array{Int64,3}:
[:, :, 1] =
 4  3  5  5
 1  1  3  5

[:, :, 2] =
 2  2  3  1
 2  5  1  1

julia> Pop[end,:,1] .≠ 1 # columns to keep
4-element BitArray{1}:
 0
 0
 1
 1

julia> Pop = view(Pop, :, Pop[end, :, 1] .≠ 1, :) # make it a view
2×2×2 view(::Array{Int64,3}, :, [3, 4], :) with eltype Int64:
[:, :, 1] =
 5  5
 3  5

[:, :, 2] =
 3  1
 1  1

julia> Pop[end,:,1] .= 1 ; # use your new view to manipulate data

julia> Pop # view of the modified Pop
2×2×2 view(::Array{Int64,3}, :, [3, 4], :) with eltype Int64:
[:, :, 1] =
 5  5
 1  1

[:, :, 2] =
 3  1
 1  1

julia> Pop.parent # original full Pop (now Pop.parent) has been modified
2×4×2 Array{Int64,3}:
[:, :, 1] =
 4  3  5  5
 1  1  1  1

[:, :, 2] =
 2  2  3  1
 2  5  1  1
于 2021-01-29T00:16:05.863 回答