How to define non-1 or arbitrary index array in Julia?

Viewed 89

I know in Julia, the index of an array begin from 1. Like

b = Array{Float64, 1}(undef, 10)

This array b is a 1d array with 10 elements. The index of b begins from 1.

But, I want an array whose index is from 0 or any integer, how to do that in Julia?

Say, I want the index ranges from 0 to 9, and I tried to do things like

b = Array{Float64, 1}(undef, 0:9)

But obviously it does not work in Julia.

Can Julia easily define an array with arbitrary index range like Fortran? I googled a little and it seems not easy to do this in Julia, am I missing something?

Is there a generic way in Julia to define arbitrary indexed array? Or do I have to install packages like OffsetArrays? It seems just not so great that Julia cannot generically define arbitrary indexed array.

Thanks!

1 Answers

In Julia, this is provided by the OffsetArrays package. Try, for example

using OffsetArrays
A = rand(10)
OA = OffsetArray(A, 0:9)
OA[0]

then

julia> OA[0]
0.26079620656304203
Related