# Broadcasting getindex for a range

**URL:** https://discourse.julialang.org/t/broadcasting-getindex-for-a-range/76937
**Category:** General Usage
**Tags:** indexing
**Created:** [February 22, 2022, 8:49pm UTC](https://discourse.julialang.org/t/broadcasting-getindex-for-a-range/76937 "2022-02-22T20:49:39Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)
#### Post date: [February 22, 2022, 8:49pm UTC](https://discourse.julialang.org/t/broadcasting-getindex-for-a-range/76937/1 "2022-02-22T20:49:39Z")

</div>

Given `vv` is a `Vector{Vector}` shouldn’t broadcasting to get a range `getindex.(vv, UnitRange)` work ?

Let’s say I have:

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

getindex.(ar, 2:3)
ERROR: DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 3 and 2")
Stacktrace:
 [1] _bcs1
   @ ./broadcast.jl:516 [inlined]
 [2] _bcs
   @ ./broadcast.jl:510 [inlined]
 [3] broadcast_shape
   @ ./broadcast.jl:504 [inlined]
 [4] combine_axes
   @ ./broadcast.jl:499 [inlined]
 [5] instantiate
   @ ./broadcast.jl:281 [inlined]
 [6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(getindex), Tuple{Vector{Vector{Int64}}, UnitRange{Int64}}})
   @ Base.Broadcast ./broadcast.jl:860
 [7] top-level scope
   @ REPL[41]:1

#also tried
getindex.(ar, [2,3])
ERROR: DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 3 and 2")
Stacktrace:
 [1] _bcs1
   @ ./broadcast.jl:516 [inlined]
 [2] _bcs
   @ ./broadcast.jl:510 [inlined]
 [3] broadcast_shape
   @ ./broadcast.jl:504 [inlined]
 [4] combine_axes
   @ ./broadcast.jl:499 [inlined]
 [5] instantiate
   @ ./broadcast.jl:281 [inlined]
 [6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(getindex), Tuple{Vector{Vector{Int64}}, Vector{Int64}}})
   @ Base.Broadcast ./broadcast.jl:860
 [7] top-level scope
   @ REPL[47]:1

#although this works
ar .|> x -> getindex(x,2:3)
3-element Vector{Vector{Int64}}:
 [2, 3]
 [5, 6]
 [8, 9]

#and ofc this works but not what needed
getindex(ar, 2:3)
2-element Vector{Vector{Int64}}:
 [4, 5, 6]
 [7, 8, 9]

```

According to this [https://github.com/JuliaLang/julia/issues/19169#issue-186396538](https://github.com/JuliaLang/julia/issues/19169#issue-186396538) I would assume that it used to be possible but now not (maybe bug) ?

tested on julia version 1.7.2

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [February 22, 2022, 8:57pm UTC](https://discourse.julialang.org/t/broadcasting-getindex-for-a-range/76937/2 "2022-02-22T20:57:30Z")

</div>

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:

```nohighlight
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
julia> getindex.(ar, Ref(2:3))
3-element Vector{Vector{Int64}}:
 [2, 3]
 [5, 6]
 [8, 9]

```
