Indexing error?

X, Y, and E are longitude, latitude and elevation, respectively, on the same 86400 x 43200 grid. My goal is to reduce their resolution by 10 folder. Why does the below code keep throwing out the below error?

my script:

m = size(X);
Ind1 = [1:10:m[1]];
Ind2 = [1:10:m[2]];

X = X[Ind1, Ind2];
Y = Y[Ind1, Ind2];
E = E[Ind1, Ind2];

Error message:

**ERROR:** LoadError: ArgumentError: invalid index: StepRange{Int64, Int64}[1:10:86391] of type Vector{StepRange{Int64, Int64}}

[1] **to_index(** I::Vector{StepRange{Int64, Int64}} **)**
@ Base ./indices.jl:297
[2] **to_index(** A::Matrix{Float64}, i::Vector{StepRange{Int64, Int64}} **)**
@ Base ./indices.jl:277
[3] **to_indices**
@ ./indices.jl:333 [inlined]
[4] **to_indices**
@ ./indices.jl:324 [inlined]
[5] **getindex(** ::Matrix{Float64}, ::Vector{StepRange{Int64, Int64}}, ::Vector{StepRange{Int64, Int64}} **)**
@ Base ./abstractarray.jl:1170
[6] top-level scope
in expression starting at test.jl:35

This is a vector that contains one element, which is a range.

Remove the braces. That should work.


Thank you so much! I didn’t know it’s that easy of a fix.

If you wanted an actual vector of indexes (which you don’t in this case), you would do:

Ind1 = collect(1:10:m[1])

Thank you for sharing!

So these three things are all different from each other?
A1 = 1:10:100;
A2 = (1:10:100);
A3 = [1:10:100];

A1 and A2 are the same. A3 is an Array with one element. Aside, if you had written A2 as (1:10:100,) it would be a Tuple with one element.


What you may be thinking of is this slightly confusing syntax, which saves a few letters over collect or vcat:

julia> [1:20:100;]  # syntax for vcat(1:10:100)
5-element Vector{Int64}:

Note also that rather than needing a name m for the size, something like X[1:10:end, 1:10:end] should figure this out for you.

1 Like