4

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
  • 1
    What is the problem with using the OffsetArrays package? Performance should be the same as for Array. Commented Jul 29, 2021 at 11:07

1 Answer 1

7

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
Sign up to request clarification or add additional context in comments.

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.