Making Definitions for Indexed Objects

In many kinds of calculations, you need to set up "arrays" that contain sequences of expressions, each specified by a certain index. One way to implement arrays in Mathematica is by using lists. You can define a list, say , then access its elements using , or modify them using . This approach has a drawback, however, in that it requires you to fill in all the elements when you first create the list.

Often, it is more convenient to set up arrays in which you can fill in only those elements that you need at a particular time. You can do this by making definitions for expressions such as .

This defines a value for .
In[1]:=
Click for copyable input
Out[1]=
This defines a value for .
In[2]:=
Click for copyable input
Out[2]=
This shows all the values you have defined for expressions associated with so far.
You can define a value for , even though you have not yet given values to and .
In[4]:=
Click for copyable input
Out[4]=
This generates a list of the values of the a[i].
In[5]:=
Click for copyable input
Out[5]=

You can think of the expression a[i] as being like an "indexed" or "subscripted" variable.

a[i]=valueadd or overwrite a value
a[i]access a value
a[i]=.remove a value
?ashow all defined values
Clear[a]clear all defined values
Table[a[i],{i,1,n}] or Array[a,n]convert to an explicit List

Manipulating indexed variables.

When you have an expression of the form , there is no requirement that the "index" i be a number. In fact, Mathematica allows the index to be any expression whatsoever. By using indices that are symbols, you can for example build up simple databases in Mathematica.

This defines the "object" with "index" to have value .
In[6]:=
Click for copyable input
Out[6]=
This adds another result to the "database".
In[7]:=
Click for copyable input
Out[7]=
Here are the entries in the database so far.
You can use these definitions wherever you want. You have not yet assigned a value for .
In[9]:=
Click for copyable input
Out[9]=
New to Mathematica? Find your learning path »
Have a question? Ask support »