Reputation: 8439
I'm writing some numerical code in which it would be more convenient to use 0-based indexing than Julia's default 1-based indexing. Julia does support 0-based indexing, but the documentation for it is aimed squarely at developers, so I'm a bit confused about how to use it.
Specifically: if I call rand(10,10)
I get a 10x10 array with indexes from 1 to 10. Is there a simple equivalent command that will return an array with indexes from 0 to 9 instead? Or alternatively, can I easily convert this to a 0-based array instead of a 1-based one?
Upvotes: 6
Views: 529
Reputation: 8439
I'm posting a self-answer because the question was answered by Bogumił Kamiński in the comments.
There is some user-level documentation here, though at the time of writing it is rather short and consists entirely of examples, so one has to infer the intended semantics and guess at best practices.
However, it seems the command
OffsetArray(rand(10,10),0:9,0:9)
achieves what I was asking for, and Bogumił Kamiński confirmed that this is the correct way to do it, so one can guess that this doesn't needlessly copy the array, etc.
One hopes that some proper documentation will be written at some point, since this is quite an important feature. (One hopes this for many important features of Julia.)
Upvotes: 7