Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does matlab have 1 based indexing [closed]

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?

like image 682
Anoop Avatar asked Mar 20 '14 22:03

Anoop


People also ask

Does MATLAB use 1 indexing?

Individual elements in an array can be accessed using a comma separated list of integer indices. In most programming languages, the first element of an array is element 0. In MATLAB, indexes start at 1.

Why is MATLAB not zero indexed?

Direct link to this answer MATLAB does not allow an index of zero into an array unless you are performing logical indexing using a vector including a logical 0 and want to ignore the corresponding element of the array into which you are indexing.

Why do we use 0 based indexing?

Zero-based indexing is a very common way to number items in a sequence in today's modern mathematical notation. In particular, the combinatorial number system uses the numbers 0 to represent empty subsets of a set when it comes to combinatorics.

How does indexing in MATLAB work?

Logical Indexing MATLAB extracts the matrix elements corresponding to the nonzero values of the logical array. The output is always in the form of a column vector. For example, A(A > 12) extracts all the elements of A that are greater than 12. Or you could replace all the spaces in a string matrix str with underscores.


1 Answers

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
[email protected]

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

like image 200
chappjc Avatar answered Nov 16 '22 00:11

chappjc