juliaarray-indexing

Subdimensional array from multidimensional array in Julia


With NumPy I can access a subdimensional array from a multidimensional array without knowing the dimension of the original array:

import numpy as np
a = np.zeros((2, 3, 4))  # A 2-by-3-by-4 array of zeros
a[0]  # A 3-by-4 array of zeros

but with Julia I am at a loss. It seems that I must know the dimension of a to do this:

a = zeros(2, 3, 4)  # A 2-by-3-by-4 array of zeros
a[1, :, :]  # A 3-by-4 array of zeros

What should I do if I don't know the dimension of a?


Solution

  • selectdim gives a view of what you are looking for,

    a = zeros(2, 3, 4)
    selectdim(a,1,1)