Select specific elements from each column of a matrix in julia

I won’t call it pretty, but you could use linear indexing

# you can overwrite test_indices with lin_indices if you want to save an allocation
lin_indices = test_indices .+ (0:size(test_indices,2)-1)' .* size(test_mat,1)
test_mat[lin_indices]

Arguably more elegant is CartesianIndex

cart_indices = CartesianIndex.(test_indices, axes(test_indices,2)')
test_mat[cart_indices]

But I would probably go with a comprehension like

[test_mat[test_indices[i,j],j] for i in axes(test_indices,1), j in axes(test_indices,2)]

or a simple for loop that fills an array.

5 Likes