Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Selecting multiple elements of an array by a list of indices

$
0
0

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!

10 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images