I have a list of indices, say inds = [[1,2], [2,3],[4,4]] that I want to use as indices into an array. For example, if
A = reshape(1:16,4,4)
then I would want to return the values [5, 10, 16]. I can, for example, use:
[A[x[1],x[2]] for x in inds]
but that seems very clumsy; I would want something more elegant, like A[inds] (which doesn’t work). What is the canonical Julia way of pulling a list of values from an array, using a list of indices to do so? And where can I find this in the documentation? It seems as though it would be a pretty standard sort of operation, but clearly I’ve either been looking in the wrong places, or misunderstanding what I’ve been reading… Anyway, thanks!
Thanks again - yes, I’ve been trying all sorts of combinations, but they return either a subarray, or an error. But I’ll check on getindex. Of course, given that I know the size of my array, I could create my indices as single values i+n*(j-1) for an n by n array. That would give me inds = [5,10,16] and then A[inds] works fine. In other words, using linear indexing instead of cartesian indexing.
Oh thank you! I had a vague inkling that CartesianIndex might be able to help, but I couldn’t see how to do it - so it’s all done with broadcasting! Very nice indeed, and again, many thanks.
Thank you - clearly the triple dots hold some sort of magic of which I’m unaware. It does work though! (I’ve just discovered that this is in fact the superbly named splat operator.)