获取 OffsetArray 的左上索引
Get the top left index of an OffsetArray
我有一个关注OffsetArray
:
julia> off = OffsetArray(rand(5, 5), -3, -3)
5×5 OffsetArray(::Matrix{Float64}, -2:2, -2:2) with eltype Float64 with indices -2:2×-2:2:
0.515173 0.861326 0.349478 0.970478 0.255713
0.862617 0.47006 0.707166 0.938883 0.331716
0.512007 0.0325946 0.553909 0.569638 0.510056
0.941383 0.351381 0.35792 0.482246 0.439157
0.887686 0.413278 0.527105 0.782516 0.976842
我想以编程方式提取左上角元素的索引,在本例中为 (-2, -2)
。
我现在正在做这个
topleft = off |> axes |> CartesianIndices |> first |> ind -> ind.I
这对我来说似乎有点矫枉过正,但我找不到任何其他选择。
有没有更直接的方法?
我通常会使用 firstindex
:
julia> firstindex(off, 1)
-2
其中第二个参数是您要获取其第一个索引的维度。
要获得 (-2, -2)
元组,您可以这样写:
julia> firstindex.(Ref(off), (1, 2))
(-2, -2)
我有一个关注OffsetArray
:
julia> off = OffsetArray(rand(5, 5), -3, -3)
5×5 OffsetArray(::Matrix{Float64}, -2:2, -2:2) with eltype Float64 with indices -2:2×-2:2:
0.515173 0.861326 0.349478 0.970478 0.255713
0.862617 0.47006 0.707166 0.938883 0.331716
0.512007 0.0325946 0.553909 0.569638 0.510056
0.941383 0.351381 0.35792 0.482246 0.439157
0.887686 0.413278 0.527105 0.782516 0.976842
我想以编程方式提取左上角元素的索引,在本例中为 (-2, -2)
。
我现在正在做这个
topleft = off |> axes |> CartesianIndices |> first |> ind -> ind.I
这对我来说似乎有点矫枉过正,但我找不到任何其他选择。
有没有更直接的方法?
我通常会使用 firstindex
:
julia> firstindex(off, 1)
-2
其中第二个参数是您要获取其第一个索引的维度。
要获得 (-2, -2)
元组,您可以这样写:
julia> firstindex.(Ref(off), (1, 2))
(-2, -2)