Question

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?

Was it helpful?

Solution 2

Yes, arrays are indexed from 1

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top