17

I used to program in Java and Python earlier, but recently have started using MATLAB for lots of stuff (specifically computer vision algorithms).

However MATLAB has indexing of arrays beginning from 1 instead of 0, which is the norm in almost every programming languages I have encountered so far.

The reason 0-based indexing made sense to me was like this: In every processor I have seen, the address index begins with 0x00000000 say in an 8-bit processor and If I want to save 5 numbers, they would be stored in address 0x00000000 to 0x00000004 . Hence it makes sense to have indexing from 0 in programming languages.

While I was searching for this question I found List of 1-indexed programming languages? and http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array) to be useful.

For me it is not a very convenient notation, maybe because I used to code in other languages before. But I still don't understand why MATLAB (and even Julia now) has a 1-based array indexing and what advantages it provides. Can anyone list out the advantages?

Community
  • 1
  • 1
Anoop
  • 5,540
  • 7
  • 35
  • 52
  • 5
    MAT(rix) LAB(oratory) generally follows mathematics/linear algebra conventions, not computer science conventions. With matrices the first element, i.e. the one in the top left corner, is the (1,1) element, not the (0,0) element, hence 1 based indexing, not 0 based indexing. – Phil Goddard Mar 20 '14 at 23:03
  • 3
    I believe this has to do with MATLAB's original underpinnings being written in Fortran, which also has 1-based indexing. This issue has been fought to death on Usenet, so Googling it should reveal more details. Anyway, I'm voting to close as opinion based since the real answer lies in Cleve Moler's head. Even if someone were to produce an authoritative reference as why this is the case, the question is still off-topic for SO. Meanwhile, if you're going to use MATLAB, get used to it and deal with it; it's what the rest of us have done. – Praetorian Mar 20 '14 at 23:04
  • In a list of numbers, say 45, 17, 94, 37 I think of the first number in the list as 45. Talking about the zeroth number makes little sense to me conceptually. For that reason alone I like 1 based indexing. As Phil said, this reflects mathematical notation, which is the primary (?) intended use for Matlab. – David Mar 20 '14 at 23:04

2 Answers2

28

Why does it have 1-based indexing? Historical reasons. (Cleve Moler decided so). It probably has to do with 1-based indexing being the convention for matrix notation.

Why does it not implement 0-based indexing too? Up for some nice light reading?

In particular, look for comments from Cleve Moler (the creator of MATLAB) and Steve Lord (MathWorks Engineer). After much exchange, the basic reasoning (at least in 2001) for not implementing a supplementary 0-based indexing was not for any philosophical or compatibility reasons, although it started as a backward compatibility argument, but because it would require just too darn much effort to update MATLAB built-in functions:

I agree that it would be possible to add zero-based indexing to MATLAB, using either new notation or an object that overloaded all of our existing indexing notation. It could be backwards compatible in the sense that old code that didn't know about or use the new stuff would still work.

But here is the key point: none of our existing code would work with the new object. You couldn't plot it; you couldn't print it; you certainly couldn't do any matrix operations with it. Yes, over time, everything could be rewritten to handle the extension, but that is what we want to avoid. It would be like AM and FM radio -- two systems living in the same box, but with separate and independent underlying technology.

-- Cleve Moler
moler@mathworks.com

Translation: They don't want to write all new plot, print, etc. routines.

chappjc
  • 30,359
  • 6
  • 75
  • 132
11

Matlab is intended as a way of making the functionality of programming languages accessible to people who haven't learnt to code. Having indices start at 0 can be a common source of bugs for people not familiar with programming, and the whole point of Matlab is to enable people to apply maths they understand to problems they need to solve, without much additional knowledge.

Most of the tasks that Matlab is used for, e.g. physics and engineering, use the convention of indices starting at 1 in most of the literature, so people working in these fields do not need to convert their algorithms by subtracting 1 from everything.

In addition to this, Matlab is short for "matrix laboratory", and in mathematics, the convention is typically to index matrices starting at 1, so it kind of makes sense that they will follow this convention.

Your rationale for why it makes sense to start at 0 is perfectly good, but it's the purpose of a high-level language like matlab to obscure aspects like these that the user does not need to know.

Zac-K
  • 550
  • 5
  • 17