Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

julia

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!

like image 347
CRquantum Avatar asked Sep 12 '25 17:09

CRquantum


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
like image 168
cbk Avatar answered Sep 14 '25 12:09

cbk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!