Specify a generic array range

Hi,

I’m building a function where as input I feed an array whose dimensions are not known a priori, but that is going to be of length 100 in each dimension. So it can be, as an input

a=rand(100) or a=rand(100,100) or a=rand(100,100,100) or ...

and I need to filter the middle elements only, so

a[45:55] or a[45:55,45:55] or a[45:55,45:55,45:55] or ...

I thought I could build up a range and use it depending on the number of dimensions of the array. In the simple case a=rand(100), this works

rng=45:55 
a[rng]

but I don’t seem to be able to generalize that to more dimensions. For instance, with a=rand(100,100), what would be the way to get a[45:55,45:55]?
I’ve tried things like

rng=45:55 
c=(rng,rng)
a[c]

but that does not work

ERROR: ArgumentError: invalid index: (45:55, 45:55) of type Tuple{UnitRange{Int64},UnitRange{Int64}}
Stacktrace:
 [1] to_index(::Tuple{UnitRange{Int64},UnitRange{Int64}}) at ./indices.jl:270
 [2] to_index(::Array{Float64,2}, ::Tuple{UnitRange{Int64},UnitRange{Int64}}) at ./indices.jl:247
 [3] to_indices at ./indices.jl:298 [inlined]
 [4] to_indices at ./indices.jl:295 [inlined]
 [5] getindex(::Array{Float64,2}, ::Tuple{UnitRange{Int64},UnitRange{Int64}}) at ./abstractarray.jl:927
 [6] top-level scope at none:0

What would be the easiest way to specify this multidimensional range?

Thanks in advance,

Ferran.

You can just splat your c into the indexing expression:

julia> a[c...]
11×11 Array{Float64,2}:
 ...
1 Like