Tutorial 2 - List, Table, Array
Han Q Le (c) -copyrighted
1. List basic
1.1 General discussion
Sometimes, data is not just one single item, for example:
Out[36]=
social network | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
market share | 0.46 | 0.29 | 0.02 | 0.04 | 0.04 | 0.06 | 0.09 |
Variable “social network” is not just one thing, but a List of things. Likewise for variable “market share”. How do we represent such a variable?
In other words, a List is a 1-dimensional array, formed by: { , , , ,...} ; Note that the separator is , NOT semiconlon ;
One way to remember the separator syntax is this: .csv: comma-separated value. In Mathematica, just like in C++, Python, semicolon ; is for terminating a statement.
There is another thing worth noticing: socialnetwork is a list of items, but each item is not a number, but an image. We will learn later on that Mathematica, like any other advanced language, deals with objects that can be anything as a single entity.
Another example, a grocery shopping List.
Out[33]=
item | unit |
juice | 3 |
eggs | 4 |
fruits | 12 |
vegetables | 8 |
milk | 2 |
cereals | 6 |
coffee | 1 |
tea | 2 |
ice cream | 3 |
napkins | 5 |
foils | 2 |
storage bags | 10 |
toothpaste | 4 |
shampoo | 3 |
detergent | 2 |
Variable “item” is the List of grocery item to be shopped, and variable “unit” is the List of number of units to be purchased.
Exercise 1.1
Define a variable groceryitem that contains data of the first column, and groceryunit that contains the data in the second column. Note that a variable value that is a string requires quotation mark, such as “storage bags”.
Answer
In[70]:=
1.2 Compared with other languages
All programming languages that are at a higher level than Assembly language allow data structure of multiple items, known as “array”. Usually it is a sequential block of memory that can be addressed by a single variable name, and the index points to the content of the position in the memory block.
Example from C++:
In Mathematica, Python, R, and some other languages, we use array with a more elevated concept: List, which is an ordered set of objects that may allow mixing of different types of variables. But first, let’s compare the syntax. Similar to C++, Mathematica uses curly brackets: {}, unlike Python with [] (A most common habit twister when switching between Mathematica and Python is the [] and () for function and {} for list).
1.3 Generalized data and variables
Any object data (or data object) can be a named variable.
In[1]:=
Out[2]=
In[3]:=
Out[4]=
Exercise 1.2
Use your computer webcam to take a picture of yourself and name “mypict”. If you don’t have one, just download from your phone or your social media page. The function to use is:
In[56]:=
After you capture an image, name it mypict, then verify you have it by calling mypict.
Answer (this is only an example - delete, replace with yours)
In[57]:=
Out[58]=
END exercise
Now, we will try a sound data object:
In[7]:=
In[8]:=
Out[8]=
In[9]:=
Here we learn a syntax of Mathematica: instead of using [], we can also use @ (which means apply, such as applying the function to the variable. Example:
In[4]:=
Out[4]=
In[5]:=
Out[5]=
Exercise 1.3 Record your student ID info
Use this function: SystemDialogInput[“RecordSound”] to record a sound object: “saying your name, and student ID” for example: “John Doe <pause> 0987654321”. Recommended setting: 22 kHz, 16-bit, mono.
then apply EmitSound[myID] to verify it you have the
data.
Check with SpeechRecognize[myID]
An alternative function is AudioCapture[]
Answer
In[50]:=
Out[50]=
In[51]:=
Out[51]=
Exercise 1.4 Synthesize your spoken student ID
We can use function Speak to generate an immediate system sound, like this
In[1]:=
However, it doesn’t give us a sound object. We can use a different function SpeechSynthesize
However, we need to choose a voice style, check your computer voice styles:
In[2]:=
Out[2]=
Now, define a variable for a string that contains your name and student ID. Then generate a speech based on that string named it myIDsynth
Answer
In[3]:=
In[5]:=
Out[5]=
In[6]:=
In[7]:=
Out[7]=
1.4 List of different types of object: concept of record, database
In[61]:=
In[43]:=
Out[43]=
Exercise 1.5 Create a list of your personal data as shown below
Answer (replace the below with your info)
In[3]:=
Out[3]=
END exercise
Additional examples:
Out[6]=
In[24]:=
Out[24]=
In[18]:=
Out[18]=
1.5 Reference elements of a List
In[66]:=
Out[67]=
One can embed the data inside a Manipulate object, so that one doesn’t have to execute statements outside or prior to the Manipulate object.
In[73]:=
In[38]:=
2. Examples of using List objects
2.1 List - graphics example 1
Here, just to change the subject to make it more interesting, we’ll do some example in business, marketing instead of just engineering.
Let’s do this exercise: generate this graphics:
2.1.1 Step 1: data
Below
are equivalent examples in R (open cell to
see -> )
END R example - resume Mathematica lecture
2.1.2 Step 2: make a PieChart3D
Exercise 2.1 make a pie chart
Make a pie chart with given list data of market share above.
Answer
In[7]:=
Out[8]=
2.1.3 Step 3: make a PieChart3D with logos as legends
Exercise 2.2 pie chart with legends
Make a pie chart of market share data with logos as legends
Answer
In[23]:=
Out[25]=
2.1.4 Step 4: use alternate color
In[67]:=
Exercise 2.3 pie chart with given color
Use option ChartStyle
Answer
In[26]:=
Out[29]=
END exercise
2.2 List - data tabulation example
2.2.1 Grid as a printout display
In[74]:=
Out[74]=
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
0.46 | 0.29 | 0.02 | 0.04 | 0.04 | 0.06 | 0.09 |
In[75]:=
Out[75]=
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
0.46 | 0.29 | 0.02 | 0.04 | 0.04 | 0.06 | 0.09 |
In[76]:=
Out[76]=
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
0.46 | 0.29 | 0.02 | 0.04 | 0.04 | 0.06 | 0.09 |
In[79]:=
Out[79]=
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
0.46 | 0.29 | 0.02 | 0.04 | 0.04 | 0.06 | 0.09 |
In[80]:=
Out[80]=
social network | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
market share | 0.46 | 0.29 | 0.02 | 0.04 | 0.04 | 0.06 | 0.09 |
In[2]:=
In[4]:=
Out[4]=
juice | eggs | fruits | vegetables | milk | cereals | coffee | tea | ice cream | napkins | foils | storage bags | toothpaste | shampoo | detergent |
3 | 4 | 12 | 8 | 2 | 6 | 1 | 2 | 3 | 5 | 2 | 10 | 4 | 3 | 2 |
In[84]:=
Out[84]=
juice | 3 |
eggs | 4 |
fruits | 12 |
vegetables | 8 |
milk | 2 |
cereals | 6 |
coffee | 1 |
tea | 2 |
ice cream | 3 |
napkins | 5 |
foils | 2 |
storage bags | 10 |
toothpaste | 4 |
shampoo | 3 |
detergent | 2 |
In[85]:=
Out[85]=
juice | 3 |
eggs | 4 |
fruits | 12 |
vegetables | 8 |
milk | 2 |
cereals | 6 |
coffee | 1 |
tea | 2 |
ice cream | 3 |
napkins | 5 |
foils | 2 |
storage bags | 10 |
toothpaste | 4 |
shampoo | 3 |
detergent | 2 |
In[86]:=
Out[86]=
item | unit |
juice | 3 |
eggs | 4 |
fruits | 12 |
vegetables | 8 |
milk | 2 |
cereals | 6 |
coffee | 1 |
tea | 2 |
ice cream | 3 |
napkins | 5 |
foils | 2 |
storage bags | 10 |
toothpaste | 4 |
shampoo | 3 |
detergent | 2 |
Later, you can learn about TableForm and MatrixForm.
Exercise 2.4 (answer given)
Find the logos of the following market indices: Dow-Jones, S&P, Nasdaq, FTSE, Nikkei, and Hang Seng and make a named list.
Answer (given)
In[4]:=
Out[5]=
Exercise 2.5 (answer given)
Make a named list of the market symbols corresponding to the above.
Answer (given)
In[72]:=
Out[73]=
Exercise 2.6 (answer given)
Make a dynamic Grid display of market index values whenever a button is pushed
Answer (given)
Out[13]=
3. Table and Array
3.1 Basic concepts
All ordered set of items are called “list” in
Mathematica. They can also be called “table” or “array”
(generic concepts). It is convenient to generate a list from a
formula or function. When we do this, the elements of the list are
usually of the same types, for example, they can be numbers,
strings, images, etc. Hence, there is really no fundamental
difference between general “list” and “array”,
except that the latter is usually used to imply that all elements
have the same type.
Below, we use Table to generate a list that can be considered as
one-dimensional array
In[1]:=
Out[1]=
Exercise 3.1
Generate a table, name it with a name of your
choice, item generated is
for index from 0 to 20 in step of 1.
Answer
In[87]:=
Out[87]=
Exercise 3.2 ListPlot
Use ListPlot and ListLinePlot
to plot your table data in 1.1 above. For ListPlot,
use this option:
PlotStyle-> {Red,PointSize[0.02]}
Put both graph into a list with a name (only one name for the
list), then use Show[“the list name”]
Answer
In[88]:=
Out[89]=
Out[90]=
Exercise 3.3 Damped oscillator
Consider this function: ;
where t is time. Let τ=0.35 sec, f=440 Hz. Calculate s(t) for t=0
to 1 sec, in step of 1/11025 sec. (11025 is known as the sampling
rate).
- Make a ListPlot of the data
- Make a sound with array s(t) that you obtain.
Answer
In[5]:=
In[91]:=
Out[91]=
Out[92]=
Out[93]=
END exercise
3.2 Range
We don’t always need to use Table to generate a most often used array: a linear array. For this we can also use Range.
In[18]:=
Out[18]=
In[19]:=
Out[19]=
What if we want to generate from -12 to 37?
In[20]:=
Out[20]=
What if we want to generate from 0.8 to 3.6 in step of 0.12
In[21]:=
Out[21]=
In[22]:=
Out[22]=
Ever since after Math 9, Mathematica Plot function can sometimes be very very slow. Hence, it is sometimes better to generate an array points to plot just like other languages. One can apply a function to a linear array:
In[94]:=
Out[95]=
Exercise 3.4
Use this code to generate a 2D vector, you must provide values of coordinate x and y for each point below.
In[96]:=
Answer
In[99]:=
Out[101]=
Exercise 3.5
Use the above code in Exc. 1.3 to generate an array
of 48 vectors like this:
start point: end
point
for n from 0 to 47. Do this by using Table that contains 48
vectors.
Answer
In[102]:=
Out[103]=
3.3 Table of Table: nested Table or 2D-array
In the above example. we have an array of vectors:
In[104]:=
Out[105]=
Suppose the function of interest has another variable, t besides x, and it is not Sin[2 π x] but Sin[2 π (x-t)]. Then to show it for different values of t, we need another dimension: we have an array depending of x for each t value. What we need is a table of table: nested table.
Exercise 3.6
Create a table in which, each element is a graphic
object of vector above, but let the function be Sin[2 π (x-t)].
The index of the outer table is t, for t from 0 to ,
in step of
.
Name the created table, but don’t print out.
Answer
In[106]:=
Exercise 3.7
Use Manipulate to show the
element of the list generated in Exc. 1.5.
Manipulate[
yourarray[[i]] , {i, 1 ,Length[ yourarray] , 1 }]
Answer
In[107]:=
Exercise 3.8
Follow instruction in class.
Answer
In[5]:=
In[6]:=
In[51]:=
Out[51]=
Exercise 3.9 Damped oscillator - with different frequencies
Use nested Table to make a table of signals similar to 3.3, but different frequencies and durations: f = {261.63, 261.63, 392, 392, 440, 440, 392}, and for each frequency, the corresponding duration is: t={0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1}. Then use Flatten to put all of them together into a single list and play.
Answer
In[51]:=
In[56]:=
Out[56]=
END exercise
3.4 Referencing elements from a multi-D array
It is straight-forward similar to other languages. First, recall what we have with an example above
In[18]:=
Out[19]=
Out[20]=
Out[21]=
Out[22]=
Now, consider this two dimensional array
In[29]:=
Out[30]=
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
In[31]:=
Out[31]=
In[66]:=
Out[66]=