Home > Software engineering >  How to get a value in a numpy julia array passing as argument a list
How to get a value in a numpy julia array passing as argument a list

Time:05-28

Let's imagine I have a numpy array in Julia A = np.array([[1,2],[3,4]])

I want to get the value at position say (1,1), which is, in Julia, 1.

I would like to pass a list as argument : I = [1,1] such that println(A[I]) returns 1 as expected.

I can't find a way to do that. In python, I know that we can pass a tuple to a numpy array, but It doesn't work in Julia.

Is there an easy was to do so?

CodePudding user response:

2 equivalent ways, both using splatting (...):

julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
 1  2
 3  4

julia> I = (1, 1)
(1, 1)

julia> A[I...]
1

julia> getindex(A, I...)
1
  • Related