Square Bracket Notation for Broadcasting getindex Across Array of Vectors?


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}},
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.

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.

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

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

See #19169 and the issues linked there.

1 Like

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