Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Julia – How to index vector w/ vector of vector of indices?

Suppose idxs is a vector of vectors defined as

3-element Vector{Vector{Int64}}:
 [1, 2]
 [1, 3]
 [1, 4]

And suppose A is a vector defined as

5-element Vector{Int64}:
 6
 7
 8
 9
 10

With Python and numpy, I could simply do (in pseudocode) A[idxs] and this would return a 2-D array containing the elements

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  [6 7]
  [6 8]
  [6 9]
]

How do I perform the same indexing in Julia, where I am indexing multiple times into a 1-dimensional vector, and retrieving a matrix of entries?

Thanks

>Solution :

The easiest solution would be to use list comprehension

julia> [A[i] for i in idxs]
3-element Vector{Vector{Int64}}:
 [6, 7]
 [6, 8]
 [6, 9]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading