2

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?

0

2 Answers 2

3

selectdim gives a view of what you are looking for,

a = zeros(2, 3, 4)
selectdim(a,1,1)
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to iterate over each "subdimensional array" in order, you can also use eachslice:

julia> a = reshape(1:24, (2, 3, 4));

julia> eachslice(a, dims = 1) |> first
3×4 view(reshape(::UnitRange{Int64}, 2, 3, 4), 1, :, :) with eltype Int64:
 1   7  13  19
 3   9  15  21
 5  11  17  23

julia> for a2dims in eachslice(a, dims = 1)
         @show size(a2dims)
       end
size(a2dims) = (3, 4)
size(a2dims) = (3, 4)


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.