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

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