How can i make Julia array index to start from zero 0 and not from one 1?

For a = [0,1,2] the index starts at a[1]=0
I want all my arrays to allways start at index zero a[0]=0.
How can configure Julia to do this?

1 Like

OffsetArrays.jl does what you are asking for.

Note that it is usually recommended to use generic indexing via for i in eachindex(arr) instead of explicitly specifying Indices. Of course 0 based indexing has its uses in particular cases but without knowing your case that is hard to assess

8 Likes

You can do this, as @Salmon says, but it is likely to cause you various issues, and also increases your chance of hitting bugs or trying to run code that only works for 1-based. Also, if you start using Tuples, or other specialized arrays, you end up back with 1-based.

I recommend just biting the bullet and using 1-based indexing. Most of the time, you don’t actually use them directly anyway. I mostly write code that uses firstindex, begin, eachindex, or simply

for val in arr
    # do something
end

You should never write

for i in 1:length(arr)
    #
end

anyway.

16 Likes

Thank you for your help.

But, maybe a general question is:
why, to beggin with, is that JULIA chose to start indexing with 1?
That for me is very un-natural in my head, for the first index is zero.

This question always opens up an endless topic which has been discussed many times.

So it’s better not to further dig-in again

Personally I am not a big user of Offset-Arrays since it’s not consistently working across the entire Julia ecosystem, so take it with caution, as mentioned above.

7 Likes

Indexing with begin (which is just syntax sugar for firstindex(a)) is basically zero-based indexing:

julia> a = [0, 1, 2]
3-element Vector{Int64}:
 0
 1
 2
 
julia> a[begin + 0]
0
10 Likes

I am fine with either zero- or one-based indexing. But to be consistent, shouldn’t you call it the zeroth index, not the first (1st)?

I’m just saying this to point out that starting at 1 is a really, really natural thing, that most people do all the time, and many programming languages do too.

24 Likes

Some people count fingers starting with the index finger as number one, while others start with the thumb; etc. However, no one starts at zero—except for someone without fingers? :slight_smile:

9 Likes

If you don’t want to dive into the links and rabbit holes, the quick version is that people have counted from 1 since antiquity, while 0 took centuries to be promoted from a placeholder at best to a bona fide number. It’s also entrenched in natural languages: “1st”, not “0th”, is how we write “first”. Many programming languages, including Julia, use 1-based indexing to represent mathematics; for example, the first element of a matrix is the intersection of the 1st row and 1st column A[1,1].

Counting from 0 in programming languages originates in offset computation; for example, the first element of an array is 0 steps away from its pointer. 1-based indexing languages actually compile to 0-based indexed code, too, but the point was to abstract the “computation quirks” away from the mathematics. Air quotes there because there are definitely a lot of mathematics where counting from 0 makes far more sense. Over time, overt 0-based indexing dominated, in large part but not entirely to C’s success.

There isn’t a real answer to which is better or more natural, just opinions and habits. Most arguments for one side have mirrored arguments for the other side. DNF is right that there’s still code out there that demand (like matrix operations) or silently assume 1-based indexing (probably an unpatched bug), so adapting to a 2nd (or 1st, if we’re counting from 0) indexing scheme is less effort in the long run. If you’re just directly indexing multidimensional arrays, OffsetArrays.jl wrappers will work, and it’s useful for not having to refactor 0-based (or whatever-based) indices to 1-based or the wordier generic firstindex/eachindex/axes code.

7 Likes

so sad… it would have been the perfect language :frowning:

Maybe a rough rule: For languages that have pointers, arrays are 0-based, because that’s the most convenient choice to index arrays?
Julia doesn’t have pointers, so its arrays are 1-based?

it does have pointers though

3 Likes

That rule doesn’t work, most languages without pointers are 0-based simply because most languages are 0-based, and a large fraction of the much fewer 1-based languages have pointers, even if it was added later or not a routine feature.

0-based or perfectly 0-based-supported Julia would still be a very imperfect language, we have Github issues, not Github “it’s fine”.

2 Likes

I agree with @stefaniecg that 0-based indexing is the objectively correct way to index. 1-based indexing has annoyed me in fortran and R and julia. However, in julia it doesn’t matter most of the time, since you usually don’t bother with it, but use eachindex, begin, first, last etc. anyway. The exception is when implementing algorithms which are written with complicated index arithmetic. Then it’s easy and performancewise cheap to wrap the arrays in OffsetArray from OffsetArrays.jl in those functions which do such arithmetic, e.g. with A = Origin(0)(_A).

1 Like

For me, I was introduced to Julia as a replacement for Matlab and R, i.e., languages that many scientists, researchers, and mathematicians use. These folks may not be used to other programming languages that are zero-based and so the jump to Julia is more manageable. Personally, it’s just more natural for me to think one-based index for mathematical research.

4 Likes

Ooooh, spicy. :hot_pepper:

We use two indexing methods all the time in our everyday life! We have counts (one-based) and distances (zero-based). And it hurts my head anytime the two collide. If you’re building a fence, the fenceposts are one-indexed (counted) whereas the distance (or number of spans from the beginning) is zero-indexed. You can ask someone to go to the fourth fencepost, or to travel three spans. If you’re building a fence, you need one more post than you do spans.

So, yeah, if you think of arrays as pointer based, with elements located some distance from the root, then yes, 0-based is indeed objectively correct. But if you think of arrays as a collection of objects, then 1-based will the natural go-to.

Perhaps the perfect example of this is in typical European vs. USA floor labeling schemes. Europeans think of how many flights to climb or descend (a distance!) whereas US Americans count the floors.

39 Likes

I’ll bite and state the obvious which has probably been mentioned a lot in similar threads.

I’ve had several programming classes where there were beginners in each of them. All of these classes were in 0-based languages. I dont recall a single time when the introduction of arrays didnt confuse a good portion of the beginners. FWIW, I myself remember quite a few bugs that I made as a beginner when incorrectly accessing the first element of an N length vector by a[1] or the last by a[N].
Arguably, the more intuitive one is one-based indexing, and that by a large margin. The only people I’ve heard stating the opposite were people with at least a year of using 0-based indexing behind them.

That being said I am sympathetic:
I agree that there are several cases where 0 based indexing makes more sense. I’ve encountered them too. Coming to julia from other languages, I missed 0 based indexing a little bit at first, then quickly found out that I dont really care about it after all.

7 Likes

I like hairdryers

29 Likes

That was of course tongue in cheek. I don’t really care how arrays are indexed, but many algorithms which require index arithmetic are described with 0-based indices. E.g. if you multiply the index by something, modulo the length of a vector, etc, etc. Or you treat a matrix as a vector of concatenated columns (or … shudder … rows). The 0/1-based discussion is as meaningless as the row/column-major matrices discussion, or various ways to represent sparse matrices. There will always be cases where one of the ways is simpler than the other, and fortunately it’s quite easy in julia to switch between the various ways.

3 Likes

For me, it really just comes down to

What’s the third element of v?

If one can answer with v[2] without even a hint of embarrassment, I’d be quite impressed

15 Likes