I seem to find that I get an bounds error when I try to reference array[0]
. So it seems that julia has elected to necessarily use the same convention that MATLAB uses, starting the indices from 1. Is this true? Any reason for that choice?

- 4,322
- 2
- 25
- 42

- 311
- 1
- 3
- 10
-
The question is phrased too rhetorically: "Is it true that...", "elected to necessarily use...". Also, the answer is not so hard to find online, e.g.:http://julia.readthedocs.org/en/latest/manual/noteworthy-differences/#noteworthy-differences-from-python – user664303 Apr 23 '14 at 12:55
-
6The correct design is to allow the user to choose the index range. This way the language is used to model the problem as best seen by the programmer, not the programmer having to model the problem as best seen by the language. See Ada for example, where you can start the index anywhere you decide. zero, 1, -100, 4, etc... You the programmer choose what is best for each problem. Not the language. http://en.wikibooks.org/wiki/Ada_Programming/Types/array – Nasser Apr 25 '14 at 02:46
3 Answers
Yes.
It is a not uncommon practice for languages to index their arrays starting 1. Check out Wikipedias page on Comparison of programming languages (array). Other notable languages that index from 1:
- Fortran
- Lua
- Mathematica
- MATLAB
- PL/1
- Smalltalk
- R
Note that half those examples are languages geared towards technical, statistical and scientific computing, just as Julia is.
When it comes to learning curve, indexing from 1 makes perfectly fine sense as the likely users will come from MATLAB, R, Fortran, et al which also index from 1.

- 1
- 1

- 38,370
- 19
- 110
- 156
Yes, arrays are indexed from 1

- 3,753
- 1
- 27
- 31
-
Thanks. Do you have any idea why the developers have chosen this rather than allowing a zero as a legal index? I don't really see why having a restriction in indexing is a feature important enough to apparently 'require' it. I can live with this but just thought I would ask...:). – comer Apr 22 '14 at 19:50
-
They had to pick something. I usually count from one (unless I'm programming C or Python), and I can not see any reason why the compiler should not do the work of translating my mental indexes to real indexes. I'm not sure I think it was a good decision to be different from C and Python though. – ivarne Apr 22 '14 at 20:11
-
Ok, thanks for the perspective. I am only complaining because I have lately wanted to use some Python functions in julia, appropriately translated, and have bumped against this silly thing. The thing is that I will need to redo a derivation of an algorithm from Python since the rule 'just increment the index by 1' has unclear consequences for the formulae I am using from Python. Just grumbling...). Thanks for the comments. – comer Apr 22 '14 at 21:26
-
I am sure you are aware of this but you can call python from within Julia-[PyCall](http://docs.julialang.org/en/release-0.2/packages/packagelist/#id229) – Francis Smart Apr 22 '14 at 23:01
-
Yep, I am aware of that. But the beauty and justification for learning julia outweighs the somewhat resistive-to-change view of sticking to one language and desperately hoping that some smart person will devise a translator so I don't have to expand my understanding. Julia, what I currently know about it, has features which Python apparently will not have and stay Python. Dispatch is one that immediately comes to mind plus Julia's inherent data structure design having so much flexibility. Thanks for your comment. – comer Apr 22 '14 at 23:41
-
1By the way, the latest release contains "Experimental support for arrays whose indexing starts from values other than 1." ([source](http://julialang.org/blog/2016/10/julia-0.5-release)). – Robert Pollak Nov 30 '16 at 14:08
According to a brief review on http://www.fortran90.org/src/faq.html#what-is-the-most-natural-starting-index-for-numbering, the most common starting index in math and science is 1, so it makes a sensible default.
Now there is experimental feature in Julia for offset array support http://docs.julialang.org/en/release-0.5/devdocs/offset-arrays/, so you may pick your own on array-per-array basis.

- 25,267
- 15
- 124
- 150