# Square Bracket Notation for Broadcasting getindex Across Array of Vectors?

**URL:** https://discourse.julialang.org/t/square-bracket-notation-for-broadcasting-getindex-across-array-of-vectors/6227
**Category:** General Usage
**Tags:** plotting, array, linearalgebra
**Created:** [October 3, 2017, 7:51pm UTC](https://discourse.julialang.org/t/square-bracket-notation-for-broadcasting-getindex-across-array-of-vectors/6227 "2017-10-03T19:51:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jessebett](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jessebett/32/2006_2.png) [@jessebett](https://discourse.julialang.org/u/jessebett)
#### Post date: [October 3, 2017, 7:51pm UTC](https://discourse.julialang.org/t/square-bracket-notation-for-broadcasting-getindex-across-array-of-vectors/6227/1 "2017-10-03T19:51:22Z")

</div>

Hi,

I’m working with an array holding 2D points so I can plot a trajectory of an iterative process.

Each point has the type `Vector{Float64}`, e.g. [1., 2.]

My array has the type `Vector{Array{Float64,1}}`,  
E.g.  
`trajectory[1:3] = Array{Float64,1}[[0.200811, 2.22703], [0.154444, 1.9618], [0.069549, 1.50583]]`

Now trying to plot with `Plots.jl` I’m looking for behaviour like Mathematica’s `ListPlot` which would put a point on the plane for each vector [x,y] in the array. In general this is what I’d like, does anyone know of a `Plots.jl` function or formula that works in this way?

I first attempted to use `scatter(trajectory)` which didn’t work. I realized I need to give it two arrays like:  
`scatter(xarray, yarray)`

This brings me to my main question here, more general than just plotting. Is there a way for me to use bracket notation to access, for example, the first index across all the vectors in my array?

`trajectory[1] = [0.200811, 2.22703]` returns the first vector in my array, as expected.  
`trajectory[:, 1] = Array{Float64,1}[[0.200811, 2.22703], [0.154444, 1.9618], ...` returns the entire array.  
`trajectory[1, :] = Array{Float64,1}[[0.200811, 2.22703]]` returns an array with just the first vector.

My solution right now is to broadcast `getindex` across the array:  
`getindex.(trajectory, 1) = [0.200811, 0.154444, ...`

Is there a better solution than this? Am I doing something really ugly by working with an array of vectors? I feel like this is a natural structure for my problem but perhaps not if the syntax isn’t clean.

---

<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: [October 3, 2017, 8:05pm UTC](https://discourse.julialang.org/t/square-bracket-notation-for-broadcasting-getindex-across-array-of-vectors/6227/2 "2017-10-03T20:05:30Z")

</div>

You can also use `first.(trajectory)` and `last.(trajectory)`.

The syntax `A.[I]` could mean several things and it’s very hard to get it to fuse with other dot syntaxes, so it has remained undefined for now.

---

<div class="post-metadata">

### Author: ![jessebett](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jessebett/32/2006_2.png) [@jessebett](https://discourse.julialang.org/u/jessebett)
#### Post date: [October 3, 2017, 8:08pm UTC](https://discourse.julialang.org/t/square-bracket-notation-for-broadcasting-getindex-across-array-of-vectors/6227/3 "2017-10-03T20:08:09Z")

</div>

Okay, thanks. I did try `A.[I]` and noticed the syntax wasn’t valid.

---

<div class="post-metadata">

### Author: ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)
#### Post date: [January 21, 2018, 9:52am UTC](https://discourse.julialang.org/t/square-bracket-notation-for-broadcasting-getindex-across-array-of-vectors/6227/4 "2018-01-21T09:52:40Z")

</div>

Can you give an example of different meanings for the vectorized getindex syntax? I don’t really see the ambiguity.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 21, 2018, 10:08am UTC](https://discourse.julialang.org/t/square-bracket-notation-for-broadcasting-getindex-across-array-of-vectors/6227/5 "2018-01-21T10:08:31Z")

</div>

See [#19169](https://github.com/JuliaLang/julia/issues/19169) and the issues linked there.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [September 13, 2019, 1:14am UTC](https://discourse.julialang.org/t/square-bracket-notation-for-broadcasting-getindex-across-array-of-vectors/6227/6 "2019-09-13T01:14:30Z")

</div>

You can define the operator `..` do broadcast `getindex` and `getfield`, see

> [@Neat way of broadcasting getindex and getfield](https://discourse.julialang.org/t/neat-way-of-broadcasting-getindex-and-getfield/28706):
>
> I often find myself wanting to broadcast getindex and getfield, which is perfectly doable, but could enjoy a nicer syntax. I figured out if I define the operator .. in the right way, I get a kind-of nice syntax for doing this without macros: a = [randn(3) for \_ in 1:4]; b = [complex(i,i+1) for i in 1:4]; (..)(x::AbstractArray,i...) = getindex.(x,i...) (..)(x::AbstractArray,i::Symbol) = getfield.(x,i) julia\> a..2 == getindex.(a, 2) true julia\> b..:re == getfield.(b, :re) true Edit: To broadca…
