conor
conor

Reputation: 123

How do I slice an array by index in julia

So say a=[[1 2];[3 4];[5 6]] and x is [1,2,1] how would I extract the vector [1,4,5]; that is, the index-x element of each row.

Upvotes: 1

Views: 571

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

You can use broadcasting. Use Ref(a) to avoid broadcasting over a:

julia>  getindex.(Ref(a),1:length(x),x)
3-element Array{Int64,1}:
 1
 4
 5

Upvotes: 2

Related Questions