My Authors
Read all threads
so @ethanhs is new to Mathematica and was wondering why array indexing starts at 1

and the reason behind it is actually pretty weird and due to some interesting language design choices. i thought y'all would enjoy it
Mathematica has a wonderfully quirky functional language that does a lot of things really well. I enjoy using it a lot, though I don't get to use it much these days
so first, some basics. arrays ("lists") in Mathematica are declared with curly braces, and are indexed with double square brackets

they have to be double square brackets because single square brackets are function application (e.g. `Sin[x]`), and parentheses are for grouping > arr = {5, 6, 7, 8}<br />
{5, 6, 7, 8}<br />
> arr[[1]]<br />
5
oh, also, Mathematica is symbolic. if it doesn't know what the value of some of your variables (x, y, z here), it's perfectly happy just leaving stuff symbolically

in fact, it won't even numerically evaluate things like square roots until you force it to (with the `N` function) > Sin[x^y + z]<br />
Sin[x^y + z]<br />
> sqrt(2)<br />
sqrt(2)<br />
> N[sqrt(2)]<br />
1.41421
so the thing is, you *can* index with zero, it just gives you something weird. it says that the zeroeth index of the array is `List`.

what's going on here is that `{5, 6, 7, 8}` is actually sugar for `List[5, 6, 7, 8]`, and the zeroeth index gets you the "head" of an expression
basically, everything in Mathematica is an expression of the form `head[arg, arg, arg, ...]`, or an atom like literals (numbers and strings) or symbols (`x`, `y`, `arr`, etc).

there's sugar for a lot of things, which you can strip away with `FullForm` if curious: > FullForm[x/y]<br />
Times[x, Power[y, -1]]<br />
> FullForm[sqrt(x)_5]<br />
Subscript[Power[x, Rational[1, 2]], 5]
what the indexing operator `[[i]]` actually does is get the i'th index of the expression, with i=0 being the "head" or the name of the "function being called"

this is very similar to how `argv` in C and $n in Bash work: argv[0] and $0 are the name of the program
this means that you can actually apply the indexing operator (which is also sugar for Part reference.wolfram.com/language/ref/P…) to *any* expression! > {Sin[x][[0]], Sin[x][[1]]}<br />
{Sin, x}<br />
> {(x/y)[[0]], (x/y)[[1]], (x/y)[[2]]}<br />
{Times, x, 1/y}
so what *is* List, even?

there's a package that lets you poke around function internals. let's use that on Sin, and see that it's defined as a built in kernel function (with some attributes). alright, makes sense Needs[ PrintDefinitions[Sin];" src="/images/1px.png" data-src="https://pbs.twimg.com/media/EYrD6xuU0AAwCU1.png">Attributes[Sin] := {Listable, NumericFunction, Protected};<br />
Sin[___] := <<kernel function>>;
what happens when we call it on List?

it seems like List is defined and has some attributes, but it's not defined *as* anything? > PrintDefinitions[List]Attributes[List] := {Locked, Protected}
remember that Mathematica is symbolic. it doesn't *need* to know how to evaluate `List`, it's perfectly happy treating it as an opaque name to potentially be pattern matched on later
and that's what's happening here! the index operator ("Part") doesn't care about lists, it cares about expressions! you can make your own list by using a different unbound head: > arr = manish[5, 6, 7, 8]<br />
manish[5, 6, 7, 8]<br />
> arr[[1]]<br />
5<br />
> arr[[2]]<br />
6
lists in mathematica aren't some special object. they're just an expression with no semantics on the head, and there's a little bit of syntax sugar for creating them
(this is reminiscent of how lists work in Lisp, for example, though in lisp it is more that "everything is just a list, including expressions" as opposed to Mathematica which is "everything is just an expression, including lists")
so, in conclusion the answer to "why does mathematica index arrays by 1" is "ARRAYS IN MATHEMATICA AREN'T REAL"

~fin~
oh, worth pointing out that matlab is the exact opposite and it really, really, REALLY wants you to use its arrays
and yes, matlab indexes at 1, and it is WRONG mathematica indexes at 1 because there is no such thing as indexing in mathematica<br />
matlab indexes at 1 because it is wrong<br />
this post was written by mathematica gang
Missing some Tweet in this thread? You can try to force a refresh.

Enjoying this thread?

Keep Current with Manish

Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!