Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

In mathematics, a matrix doesn't have an 'offset' or a 'starting point.' I think that perceiving matrices in those terms is an artifact of thinking of matrices as sitting in an address space, where the elements of your matrix are part of a larger span that can contain other data.

A matrix is a collection of elements and nothing more. Indexing starts at 1 because that's the first element in your matrix, and numbering the first element 1 makes sense. Numbering from 0 makes sense when it represents an offset into something, but a matrix isn't that. 0-based indexing just always feels to me like letting the implementation details leak out. (I don't feel this way when an array actually represents a chunk of memory, rather than a math object.)

The proliferation of +1 and -1 depends on the application. Some work better with 1-indexed, some work better with 0-indexed. Personally I get annoyed at having to use `len-1` too often when working with 0-indexed arrays. This is why some languages like Julia (and FORTRAN apparently?) let you choose your index-base, which... has trade-offs.



view as:

> and numbering the first element 1 makes sense

Why?


Ever heard someone talk about their zeroth kid, without making a programming joke? English counts objects one two three. I've never heard of a natural language doing otherwise, we would surely translate what they say when pointing at two apples as "two".

Mathematicians of course label objects however suits the problem at hand. It's extremely common to write say c_0 + c_1 x + c_2 x^2 + ... for a taylor series starting with a constant, or start at -1 or -n if that's tidier.


No, but mathematics is not spoken language... the origin of the number line happens to be at 0, not at 1. If you start indexing at 1, you're missing a piece. That may not be noticeable if all you do is index a fixed size thing with a manually chosen index, but as soon as you need to compute with the indices themselves, you notice it, and it's not pretty.

I'm not going to say "zeroeth" in spoken language, but for any serious computation, you're going to be indexing variable sized things and need to compute the indices themselves. And I don't want to make code or formulas less readable due to shifts by 1.


Like I said, what's most readable is problem-dependent. Sometimes it's about modular arithmetic, but sometimes it's about not having to say G(n+1) too often.

The number line concerns real numbers, not elements.


This discussion provided me some food for thought. You (and the Matlab, and many other like-minded individuals) have chosen is to work with natural numbers¹ as indexes as these provide the most value for the amount of hassle. Having a 0 based index model is not really a choice the moment where one gets forced to work with the indexes themselves. That is especially when there are negative indexes to be considered, like we happen to encounter in the real world. What is the chance to go on past the integers, having a proportion or some irrational number² like p as index?

¹ https://en.m.wikipedia.org/wiki/Natural_number

² https://en.m.wikipedia.org/wiki/Irrational_number


It's how people count things. If you want to know how many sheep or apples are in front of you, you start counting at one, and the number you get up to is the number that there are. If there are i elements of a set, the first element is the 1st element and the last element is the ith element. Doing it otherwise risks fencepost errors.

(There are perfectly reasonable arguments why computer languages might be zero-indexed, whereby objects start at the zeroth offset. But counting from 1 upwards is definitely more natural for counting, it's disingenuous to pretend otherwise).


Legal | privacy