Broadcasting getindex for a range

Broadcast is trying to match the shape of ar (length 3) with 2:3 (length 2). It might become more obvious how it’s working with the range 1:3 instead:

julia> ar = [[1,2,3], [4,5,6], [7,8,9]];

julia> getindex.(ar, 1:3)
3-element Vector{Int64}:
 1
 5
 9

In this case, you’re matching the elements of 1:3 with the three elements of ar. What you’re after, though, is to repeat the 2-element range 2:3 three times to match up with the shape of ar. To do this, make the range an element itself! You can really put it into any 1-element container:

julia> getindex.(ar, Ref(2:3))
3-element Vector{Vector{Int64}}:
 [2, 3]
 [5, 6]
 [8, 9]
3 Likes