How to create a zero-based array?

I’d like to create a multidimensional array whose indexes start at 0 instead of 1. I know that this is possible, but I can’t seem to find a simple example of how.

This page of the official documentation starts out promising, explaining that Julia supports arbitrary indexing, but then it continues with “The purpose of this page is to address the question, ‘what do I have to do to support such arrays in my own code?’”. The rest of it is technical details on how to make code compatible with arbitrary-indexed arrays, but it doesn’t seem to give an example of how to create such an array from the user end.

On the other hand, the page on array indexing doesn’t mention it at all as far as I can tell.

So, what’s a simple example of how to create, say, a 10x10 matrix A whose entries run from A[0,0] to A[9,9] instead of A[1,1] to A[10,10]?

P.S. let’s avoid discussing opinions about the topic if possible!

1 Like
julia> using OffsetArrays

julia> N = 10
10

julia> ℱ = OffsetArrays.Origin(0)([exp(-2π*im*m*n/N) for m=0:N-1, n=0:N-1])
10×10 OffsetArray(::Matrix{ComplexF64}, 0:9, 0:9) with eltype ComplexF64 with indices 0:9×0:9:
 1.0-0.0im        1.0-0.0im                1.0-0.0im          …        1.0-0.0im
 1.0-0.0im   0.809017-0.587785im      0.309017-0.951057im         0.809017+0.587785im
 1.0-0.0im   0.309017-0.951057im     -0.809017-0.587785im         0.309017+0.951057im
 1.0-0.0im  -0.309017-0.951057im     -0.809017+0.587785im        -0.309017+0.951057im
 1.0-0.0im  -0.809017-0.587785im      0.309017+0.951057im        -0.809017+0.587785im
 1.0-0.0im       -1.0-1.22465e-16im        1.0+2.44929e-16im  …       -1.0-1.10218e-15im
 1.0-0.0im  -0.809017+0.587785im      0.309017-0.951057im        -0.809017-0.587785im
 1.0-0.0im  -0.309017+0.951057im     -0.809017-0.587785im        -0.309017-0.951057im
 1.0-0.0im   0.309017+0.951057im     -0.809017+0.587785im         0.309017-0.951057im
 1.0-0.0im   0.809017+0.587785im      0.309017+0.951057im         0.809017-0.587785im

julia> ℱ[0,0]
1.0 - 0.0im
3 Likes