numpy中的np.indices的julia等效项是什么?

时间:2019-02-06 02:05:19

标签: julia

在Python中,有np.indices,它返回网格的索引:

Python 2.7.1
> import numpy as np
> x,y = np.indices((2,2))
> x
array([[0, 0],
       [1, 1]])
> y
array([[0, 1],
       [0, 1]])

在Julia中类似的功能是什么?尤其是对于多维网格。

我尝试了eachindex,但它希望将网格作为输入,而不仅仅是尺寸。另外,输出是线性索引的平面列表,而不是单独的笛卡尔分量。

1 个答案:

答案 0 :(得分:3)

可以使用CartesianIndices函数获得它:

julia> inds = CartesianIndices((2,2))
2×2 CartesianIndices{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}:
 CartesianIndex(1, 1)  CartesianIndex(1, 2)
 CartesianIndex(2, 1)  CartesianIndex(2, 2)

julia> Tuple.(inds)
2×2 Array{Tuple{Int64,Int64},2}:
 (1, 1)  (1, 2)
 (2, 1)  (2, 2)

julia> getindex.(inds, 1)
2×2 Array{Int64,2}:
 1  1
 2  2

julia> getindex.(inds, 2)
2×2 Array{Int64,2}:
 1  2
 1  2