4 ms·
Starting at 1 is more intuitive, and therefore IMO better. Regarding some of the advantages of zero-based: - Indexing backwards from the "end". Your languag
by jkabrg 8y ago
Starting at 1 is more intuitive, and therefore IMO better.
Regarding some of the advantages of zero-based:
- Indexing backwards from the "end". Your language can always add an `end` keyword like Matlab does, and this stops
being an advantage.
- Indexing cyclically using modular arithmetic. Yes, this is an advantage. Albeit a rare one for me.
I commit less off-by-one errors with 1-based, and I don't have to double-check as much -- so on balance, I prefer it.
[edit]
The amount of karma this comment is getting is undergoing something like Brownian motion.
- camelCaseOfBeer 8y agoHeretic.
- IshKebab 8y agoMatlab uses 1-based indexing. I've used it extensively and it was definitely a bad move. One particularly annoying example is splitting an array into blocks, e.g. for i = 1:numel(s) foo(s[i]) end That is fine, but what if you want to process in blocks of N? Now you have to do: for i = 1:(numel(s)/10) foo(s[(i-1)*N + (1:N)]) end Ugh no thanks. This is as simple as it gets too. For more complex array manipulation... enjoy adding and subtracting your 1's. 0-based indexing is just way more natural.
- SiempreViernes 8y agoI would reshape s since foo would anyway be vectorized. You could also do for i = 1:10:length(s) foo(s(i:i+9)) endfor With numpy the 9 turns into a 10 by the design choice that s[a:b] only goes to s[b-1], this is sometimes convenient, and sometimes it costs a +1.
- Serow225 8y agoYes probably reshape would be the natural MATLAB way to do it. Under the hood it doesn't change the memory allocation of the mxArray, just the metadata -- so the reshape takes virtually no time. But if you wanted to do the loop like that, I think you could simplify that to 1:10:end , btw. Also there is a blockproc function that will do it for you, you just pass in the chunk size and a function handle to 's', but it's in the Image Processing toolbox
- umanwizard 8y ago> Starting at 1 is more intuitive, and therefore IMO better. This is totally subjective. Starting at 0 is more intuitive to me. My intuition is very simple. `x` is the name of some memory location. `x[k]` is the location `k` spaces away from `x`.
- throwawaymath 8y agoI can agree that's intuitive, but I'd like to point out that's not quite the same thing as numbering a sequence. If we have a sequence a_1, a_2, a3, ... we can talk about a_3 by calling it the third term. If we have a sequence a_0, a_1, a_2, ..., the third term is actually a_2. Whether or not we should index starting at 0 or 1 is probably dependent not only on intuition, but the application at hand. For most analytic purposes it's generally more useful to talk about the nth term, and we don't need to know a specific number to reason about the distance between any two indices. For other purposes, such as programmatic ones, it is useful to know e.g. the traversal distance between two items in a list. In my opinion it's best to first consider whether you're working in more of a mathematical or programmatic context, and then secondarily who will have to read it later on.
- umanwizard 8y agoYou don't have to talk about memory locations, or computers at all, for the intuition to work. `a_2` is two spaces away from the beginning of the list. The fact that our ordinal numbers are closely connected with the off-by-one cardinal numbers (e.g., "third", meaning the element of a sequence in position 2, is closely etymologically related to the word "three") is an unfortunate defect of language.
- notduncansmith 8y ago> we can talk about a_3 by calling it the third term It's still the "third" term. People commonly refer to the "zeroeth" term in a list as the "first" term, the "first" term as the "second", and so on. Admittedly, the usefulness of "zeroeth" depends on how often you think about the mechanics of array traversal, and that's probably not often if you don't program computers.
- 8y ago
- bena 8y agoIntuition is the name we give to what we learned early. 0 and 1 are both perfectly fine starting points. They both have advantages and disadvantages. 0 is more common since a lot of languages use C as a starting point in some fashion. C chose 0 because then it can create syntactic sugar for sequential memory access via pointers. Or arrays.
- SiempreViernes 8y agoNo, C got that from B that got it from BCPL, which has that notation to compile faster: http://exple.tive.org/blarg/2013/10/22/citation-needed/ http://exple.tive.org/blarg/2013/10/22/citation-needed/
- bena 8y agoNice. It goes deeper than I've been told.
- deleted 8y ago[deleted]
- jacobolus 8y ago> Indexing backwards from the "end". Your language can always add an `end` keyword like Matlab does, and this stops Having used Matlab a bunch, in practice this sucks, because “end” is treated as this weird special case in the language grammar, and many reasonable and convenient expectations of syntax that should work turn out not to. Folks writing complicated Matlab projects end up needing to work around it, and the workarounds are brittle and confusing. Using negative integers is a whole lot easier to reason about and work with. Everyone learns in grade school how to do arithmetic with negative integers (adopted by European mathematicians in the 17th century). Even Matlab experts don’t always understand Matlab ‘end’ arithmetic.
- tomtimtall 8y agoDo you have any examples? In all my years of teaching and working with matlab I have literally never seen any student or code that showed such issues so I am very curious?
- drb91 8y ago> Starting at 1 is more intuitive Only when counting. Why should 'numbering' be the same as 'counting'? I don't see why that would lead to a generally more intuitive when most of what you calculate with 'numbering' is offsets and indexes. Offsets begin at zero. Indexes could be by ordinal and not by offset, I suppose, but I don't see how that could be a benefit in typical index calculations--mostly they're just offsets in my world, distances between indexes. I rarely ever refer to specific, literal indexes, so I can't say that I have much of an opinion on them, but that does seem to be the area where indexing by ordinal would make the most sense. Perhaps this is the main operation that dominates numerical calculations? Rails even adds english ordinal methods (e.g. first, second, third, fourth, fifth) for this purpose.