LISTS
WOLFRAM LANGUAGE MATHEMATICA
MAKING LISTS OF OBJECTS
• In doing calculations, it is often convenient to collect together several objects, and treat them as a
single entity. Lists give you a way to make collections of objects in the wolfram language. As you
will see later, lists are very important and general structures in the wolfram language.
• A list such as {3,5,1} is a collection of three objects. But in many ways, you can treat the whole
list as a single object. You can, for example, do arithmetic on the whole list at once, or assign the
whole list to be the value of a variable
MAKING LISTS OF OBJECTS
HERE IS A LIST OF THREE NUMBERS:
• IN[1]:= {3, 5, 1}
• OUT[1]= {3, 5, 1}
THIS SQUARES EACH NUMBER IN THE LIST, AND ADDS 1 TO IT:
• IN[2]:= {3, 5, 1}^2 + 1
• OUT[2]={10,26,2}
THIS TAKES DIFFERENCES BETWEEN CORRESPONDING ELEMENTS IN THE TWO LISTS. THE LISTS MUST BE THE SAME
LENGTH:
• IN[3]:= {6, 7, 8} - {3.5, 4, 2.5}
• OUT[3]={2.5,3,5.5}
THE VALUE OF % IS THE WHOLE LIST:
• IN[4]=%
• OUT[4]={2.5,3,5.5}
MANIPULATING ELEMENTS OF LISTS
• Many of the most powerful list manipulation operations in the wolfram language treat whole lists
as single objects. Sometimes, however, you need to pick out or set individual elements in a list.
• You can refer to an element of a wolfram language list by giving its "index". The elements are
numbered in order, starting at 1.
• THIS EXTRACTS THE SECOND ELEMENT OF THE LIST:
• IN[1]:= {5, 8, 6, 9}[[2]]
• OUT[1]= 8
• THIS EXTRACTS A LIST OF ELEMENTS:
• IN[2]:= {5, 8, 6, 9}[[{3, 1, 3, 2, 4}]]
• OUT[2]= {6,5,6,8,9}
• THIS ASSIGNS THE VALUE OF V TO BE A LIST:
• IN[3]:= V = {2, 4, 7}
• OUT[3]= {2,4,7}
• YOU CAN EXTRACT ELEMENTS OF V:
• IN[4]:= V[[2]]
• OUT[4]= 4
• By assigning a variable to be a list, you can use wolfram language lists much like "arrays" in other
computer languages. Thus, for example, you can reset an element of a list by assigning a value
to v[[i]].
• HERE IS A LIST:
• IN[5]:= V = {4, -1, 8, 7}
• OUT[5]= {4, -1, 8, 7}
• THIS RESETS THE THIRD ELEMENT OF THE LIST:
• IN[6]:= V[[3]] = 0
• OUT[6]=0
• NOW THE LIST ASSIGNED TO V HAS BEEN MODIFIED:
• IN[7]:= V
• OUT[7]= {4, -1, 0, 7}
GETTING PIECES OF LISTS
We will use this list for the examples:
In[5]:= t = {a, b, c, d, e, f, g}
Out[5]= {a, b, c, d, e, f, g}
Here is the last element of t:
In[2]:= Last[t]
Out[2]= g
This gives the third element:
In[3]:= t[[3]]
Out[3]= c
This gives the list of elements 3 through 6:
In[6]:= t[[3 ;; 6]]
Out[6]= {c, d, e, f,}
This gives a list of the first and fourth elements:
In[4]:= t[[{1, 4}]]
Out[4]= {a, d}
• This gives the first three elements of the list t defined above:
• t = {a, b, c, d, e, f, g}
• In[5]:= Take[t, 3]
• Out[5]= {a, b, c}
• This gives the last three elements:
• In[6]:= Take[t, -3]
• Out[6]= {e, f, g}
• This gives elements 2 through 5 inclusive:
• In[7]:= Take[t, {2, 5}]
• Out[7]= { b, c, d, e}
• This gives t with the first element dropped:
• In[9]:= Rest[t]
• Out[9]= { b, c, d, e, f, g}
• This Gives T With Its First Three Elements Dropped:
• In[10]:= Drop[t, 3]
• Out[10]= {d, e, f, g}
• This Gives T With Only Its Third Element Dropped:
• In[11]:= Drop[t, {3, 3}]
• Out[11]= {a, b, d, e, f, g}
TESTING AND SEARCHING LIST ELEMENTS
• The wolfram system also has functions that search and test for
elements of lists, based on the values of those elements.
• This gives a list of the positions at which a appears in the list:
• In[1]:= Position[{a, b, c, a, b}, a]
• Out[1]= {{1},{4}}
• Count counts the number of occurrences of a:
• In[2]:= Count[{a, b, c, a, b}, a]
• Out[2]= 2
•This shows that a is an element of {a,b,c}:
•In[3]:= MemberQ[{a, b, c}, a]
•Out[3]= True
•On the other hand, d is not:
•In[4]:= MemberQ[{a, b, c}, d]
•Out[4]= False
ADDING, REMOVING, AND MODIFYING LIST ELEMENTS
• This Gives A List With X Prepended:
• In[1]:= Prepend[{a, b, c}, x]
• Out[1]= {x,a,b,c}
• This Inserts X So That It Becomes Element Number 2:
• In[2]:= Insert[{a, b, c}, x, 2]
• Out[2]= {a,x,b,c}
• This Interleaves X Between The Entries Of The List:
• In[1]:= Riffle[{a, b, c}, x]
• Out[1]= {a,x,b,x,c}
• This Replaces The Third Element In The List With X:
• In[3]:= ReplacePart[{a, b, c, d}, 3 -> x]
• Out[3]= {a,b,x,d}
COMBINING LISTS
• Join concatenates any number of lists together:
• In[1]:= Join[{a, b, c}, {x, y}, {t, u}]
• Out[1]={a,b,c,x,y,t,u}
• Union combines lists, keeping only distinct elements:
• In[2]:= Union[{a, b, c}, {c, a, d}, {a, d}]
• Out[2]={a,b,c,d}
• Riffle combines lists by interleaving their elements:
• In[1]:= Riffle[{a, b, c}, {x, y, z}]
• Out[1]={a,x,b,y,c,z}
ORDERING IN LISTS
• Here is a list of numbers:
• In[50]:= t = {17, 21, 14, 9, 18}
• Out[50]= {17, 21, 14, 9, 18}
• This gives the elements of t in sorted order:
• In[51]:= Sort[t]
• Out[51]= {9,14,17,18,21}
• This gives the positions of the elements of t, from the position of the smallest to that of the largest:
• In[54]:= Ordering[t]
• Out[54]= {4,3,1,5,2}
• This is the same as Sort[t]:
• In[55]:= t[[%]]
• Out[55]= {9,14,17,18,21}
• This gives the smallest element in the list:
• In[2]:= Min[t]
• Out[2]= 9
• Plot The List Of Numbers {1, 1, 2, 2, 3, 4, 4}:
• In[2]:= ListPlot[{1, 1, 2, 2, 3, 4, 4}]
• Plot the list of numbers {10, 9, 8, 7, 3, 2, 1}:
• In[3]:= ListPlot[{10, 9, 8, 7, 3, 2, 1}]
In the list plot :
Vertical (y)= {1,1,2,2,3,4,4}
Horizontal (x)= {1,2,3,4,5,6,7}
list is indexes
• Generate a list of numbers, then plot it:
• In[5]:=ListPlot[Range[20]]
Reverse the elements in a list:
In[6]:= Reverse[{1, 2, 3, 4}]
In[6]:={4,3,2,1}
EXERCISES
• Reverse[Range[10]]
• Make a list of numbers up to 100.
• Make a list of numbers from 1 to 50 in reverse order
Making
Tables/Vectors/Matrix/Sets
Wolfram Mathematica
Making Tables
 Table makes a list with a single element repeated some specified number of
times.
 Make a list that consists of 5 repeated 10 times:
 In[1]:= Table[5, 10]
 Out[1]= {5,5,5,5,5,5,5,5,5,5}
 This makes a list with x repeated 10 times:
 In[2]:= Table[x, 10]
 Out[2]= {x,x,x,x,x,x,x,x,x,x}
 You can repeat lists too:
 In[3]:= Table[{1, 2}, 10]
 Out[3]= { {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2} }
 Make a table that gives the value of n+1 when n goes from 1 to 10:
 In[6]:= Table[n + 1, {n, 10}]
 Out[6]= {2,3,4,5,6,7,8,9,10,11}
 Make a table of the first 10 squares:
 In[7]:= Table[n^2, {n, 10}]
 Out[7]= {1,4,9,16,25,36,49,64,81,100}
 Here’s a table of successively longer lists produced by Range:
 In[8]:= Table[Range[n], {n, 5}]
 Out[8]= {{1},{1,2},{1,2,3},{1,2,3,4,},{1,2,3,4,5}}
 In[9]=TableForm[%]
 Out[9]=
 1
 1 2
 1 2 3
 1 2 3 4
 1 2 3 4 5
 Here each of the lists produced is shown as a column:
 In[9]:= Table[Column[Range[n]], {n, 8}]
This generates a table with n going from 4 to 10:
In[15]:= Table[f[n], {n, 4, 10}]
Out[15]={f[4],f[5], f[6], f[7], f[8], f[9], f[10],}}
This makes n go from 4 to 10 in steps of 2:
 In[16]:= Table[f[n], {n, 4, 10, 2}]
 Out[16]={f[4],f[6], f[8], f[10],}
 This generates 20 independent random integers with size up to 10:
 In[22]:= Table[RandomInteger[10], 20]
 Out[22]={3,1,4,3,6,7,6,10,9,2,1,4,5,8,3,8,3,8,3,0}
 The Wolfram Language emphasizes consistency, so for example Range is set
up to deal with starting points and steps just like Table.
 Generate the range of numbers 4 to 10:
 In[17]:= Range[4, 10]
 Out[17]={4,5,6,7,8,9,10}
 Generate the range of numbers 4 to 10 going in steps of 2:
 In[18]:= Range[4, 10, 2]
 Out[18]={4,6,8,10}
 In[11]:=Table[PieChart[Table[1,n]],{n,5}]
 Out[11]=
 Table[PieChart[Table[1, n]], {n, 8}]
Exercises
1) Make a list in which the number 1000 is repeated 5 times.
2) This makes a list with x repeated 7 times:
3) This makes n go from 4 to 20 in steps of 2:
4) Make a table of the first 8 squares:
5) Table[Range[n], {n, 10}]
6) TableForm[%]
7) Table[Column[Range[n]], {n, 4,10,2}]
8) Table[PieChart[Table[1,n]],{n,3}]
Vector Operations
 This is a vector in three dimensions:
 In[14]:= v = {1, 3, 2}
 Out[14]={1, 3, 2}
 This gives a vector u in the direction opposite to v with twice the magnitude:
 In[15]:= u = -2 v
 Out[15]={-2, -6, -4}
making of lists,tables,matrix,vactor,sets.pptx
Multiplying Vectors
 This multiplies each element of the vector by the scalar k:
 In[1]:= k {a, b, c}
 Out[1]= {ak, bk, ck}
 The "dot" operator gives the scalar product of two vectors:
 In[2]:= {a, b, c} . {ap, bp, cp}
 Out[2]= aap, bbp, ccp
 You can also use dot to multiply a matrix by a vector:
 In[3]:= {{a, b}, {c, d}} . {x, y}
 Out[3]= {ax+by,cx+dy}
 Dot is also the notation for matrix multiplication in the Wolfram Language:
 In[4]:= {{a, b}, {c, d}} . {{1, 2}, {3, 4}}
 Out[4]= {{a+3b,2a+4b},{c+3d,2c+4d}}
 Here are definitions for a matrix m and a vector v:
 In[5]:= m = {{a, b}, {c, d}}; v = {x, y}
 Out[5]= {x, y}
 This left‐multiplies the vector v by m. The object v is effectively treated as a
column vector in this case:
 In[6]:= m . v
 Out[6]= {ax+by,cx+dy}
 You can also use dot to right‐multiply v by m. Now v is effectively treated as a
row vector:
 In[7]:= v . m
 Out[7]= {ax+cy,bx+dy}
 You can multiply m by v on both sides to get a scalar:
 In[8]:= v . m . v
 Out[8]= x(ax+cy)+y,(bx+dy)
Matrix
 MatrixForm formats arrays using standard matrix formatting:
 In[1]= m = {{a, b, c}, {d, e, f}};
 In[2]= MatrixForm[m]
 When an input evaluates to MatrixForm[expr], MatrixForm does not appear in
the output:
 In[1]:= MatrixForm[{{1, 2}, {3, 4}}]
 This creates a matrix that only has nonzero entries on the diagonal:
 In[2]:= DiagonalMatrix[{1, 2, 3, 4}] // MatrixForm
making of lists,tables,matrix,vactor,sets.pptx
making of lists,tables,matrix,vactor,sets.pptx
making of lists,tables,matrix,vactor,sets.pptx
making of lists,tables,matrix,vactor,sets.pptx
SETS
Wolframe Mathematica
SETS
making of lists,tables,matrix,vactor,sets.pptx
Union
 Give a sorted list of distinct elements:
 In[1]:= Union[{1, 2, 1, 3, 6, 2, 2}]
 Out[1]= {1,2,3,6}
 Give a sorted list of distinct elements from all the lists:
 In[1]:= Union[{a, b, a, c}, {d, a, e, b}, {c, a}]
 Out[1]= {a,b,c,d,e}
 Give a list of the distinct lists:
 In[1]:= Union[{{1, 2}, {1, 2, 3}}, {{2, 1}, {1, 2}}, {{3, 2, 1}, {1, 2, 3}}]
 Out[1]= {{1, 2}, {2, 1}, {1, 2, 3},{3, 2, 1}}
 Enter using un:
 In[1]:= {a, b, c} Ս {b, c, d}
 Out[1]= {a,b,c,d}
Intersection & Complement
 Find elements common to all the lists given:
 In[1]:= Intersection[{1, 1, 2, 3}, {3, 1, 4}, {4, 1, 3, 3}]
 Out[1]= {1,3}
 Enter using inter:
 In[1]:= {a, b, c} Ո {b, c, d}
 Out[1]= {b,c}
 Find which elements in the first list are not in any of the subsequent lists:
 In[1]:= Complement[{a, b, c, d, e}, {a, c}, {d}]
 Out[1]= {b,e}
 The result of Complement[list1,list2] is sorted and does not contain repeated
elements:
 In[1]:= Complement[{b, e, d, a, b, c, d}, {b, c}]
 Out[1]= {a,d,e}
Subsets
 This gives all the subsets of the list:
 In[4]:= Subsets[{a, b, c}]
 Out[4]= {{},{a},{b},{c},{a,b},{a,c}.{b,c},{a,b,c}}
 Delete Duplicates deletes all duplicate elements from the list:
 In[5]:= DeleteDuplicates[{a, b, c, a}]
 Out[5]= {a,b,c}
 All possible subsets (power set):
 In[6]:= Subsets[{a, b, c}]
 Out[6]={{}, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}
All possible subsets containing up to 2 elements:
 In[7]:= Subsets[{a, b, c, d}, 2]
 Out[7]={{}, {a}, {b}, {c}, {d}, {a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d}}
Subsets containing exactly 2 elements:
 In[8]:= Subsets[{a, b, c, d}, {2}]
 Out[8]={{a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d}}
The first 5 subsets containing 3 elements:
 In[9]:= Subsets[{a, b, c, d, e}, {3}, 5]
 Out[9]={{a, b, c}, {a, b, d}, {a, b, e}, {a, c, d}, {a, c, e}}

More Related Content

PDF
python_avw - Unit-03.pdf
PPTX
introduction to set theory and logic theory.pptx
PPT
Matlab Overviiew 2
PPTX
Python lists
PPTX
NumericalAlgebraic Calculations, updated.pptx
PDF
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
PPTX
Matlab ch1 (3)
python_avw - Unit-03.pdf
introduction to set theory and logic theory.pptx
Matlab Overviiew 2
Python lists
NumericalAlgebraic Calculations, updated.pptx
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Matlab ch1 (3)

Similar to making of lists,tables,matrix,vactor,sets.pptx (20)

PPTX
Discrete Math Chapter 2: Basic Structures: Sets, Functions, Sequences, Sums, ...
PPT
Theory of the sets and Venn diagram-grade 9 math
PPTX
Vectormaths and Matrix in R.pptx
PPTX
NumPy_Broadcasting Data Science - Python.pptx
PPTX
Pythonlearn-08-Lists.pptx
PPTX
Lists.pptx
PPTX
Discrete Structure Mathematics lecture 1
PPTX
Python list tuple dictionary .pptx
PPTX
Pythonlearn-08-Lists (1).pptxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
PPT
Programming in Python Lists and its methods .ppt
PPTX
WRITING ON SETS AND OPERATION ON SETssss
PDF
Python Variable Types, List, Tuple, Dictionary
PPT
Multi dimensional arrays
PPT
DAA (Unit-2) (ii).ppt design analysis of algorithms
PPTX
MATLAB - Arrays and Matrices
PPTX
Chapter 2 =Introduction to Mathematica.pptx
PPTX
2. Array in Data Structure
PDF
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
PPT
Lecture in Sets, Sequences and Summations
Discrete Math Chapter 2: Basic Structures: Sets, Functions, Sequences, Sums, ...
Theory of the sets and Venn diagram-grade 9 math
Vectormaths and Matrix in R.pptx
NumPy_Broadcasting Data Science - Python.pptx
Pythonlearn-08-Lists.pptx
Lists.pptx
Discrete Structure Mathematics lecture 1
Python list tuple dictionary .pptx
Pythonlearn-08-Lists (1).pptxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
Programming in Python Lists and its methods .ppt
WRITING ON SETS AND OPERATION ON SETssss
Python Variable Types, List, Tuple, Dictionary
Multi dimensional arrays
DAA (Unit-2) (ii).ppt design analysis of algorithms
MATLAB - Arrays and Matrices
Chapter 2 =Introduction to Mathematica.pptx
2. Array in Data Structure
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
Lecture in Sets, Sequences and Summations
Ad

Recently uploaded (20)

PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
Empowerment Technology for Senior High School Guide
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
International_Financial_Reporting_Standa.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Computer Architecture Input Output Memory.pptx
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
Complications of Minimal Access-Surgery.pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
TNA_Presentation-1-Final(SAVE)) (1).pptx
Empowerment Technology for Senior High School Guide
What if we spent less time fighting change, and more time building what’s rig...
International_Financial_Reporting_Standa.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
Computer Architecture Input Output Memory.pptx
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
AI-driven educational solutions for real-life interventions in the Philippine...
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Uderstanding digital marketing and marketing stratergie for engaging the digi...
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
History, Philosophy and sociology of education (1).pptx
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Complications of Minimal Access-Surgery.pdf
B.Sc. DS Unit 2 Software Engineering.pptx
Weekly quiz Compilation Jan -July 25.pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Ad

making of lists,tables,matrix,vactor,sets.pptx

  • 2. MAKING LISTS OF OBJECTS • In doing calculations, it is often convenient to collect together several objects, and treat them as a single entity. Lists give you a way to make collections of objects in the wolfram language. As you will see later, lists are very important and general structures in the wolfram language. • A list such as {3,5,1} is a collection of three objects. But in many ways, you can treat the whole list as a single object. You can, for example, do arithmetic on the whole list at once, or assign the whole list to be the value of a variable
  • 3. MAKING LISTS OF OBJECTS HERE IS A LIST OF THREE NUMBERS: • IN[1]:= {3, 5, 1} • OUT[1]= {3, 5, 1} THIS SQUARES EACH NUMBER IN THE LIST, AND ADDS 1 TO IT: • IN[2]:= {3, 5, 1}^2 + 1 • OUT[2]={10,26,2} THIS TAKES DIFFERENCES BETWEEN CORRESPONDING ELEMENTS IN THE TWO LISTS. THE LISTS MUST BE THE SAME LENGTH: • IN[3]:= {6, 7, 8} - {3.5, 4, 2.5} • OUT[3]={2.5,3,5.5} THE VALUE OF % IS THE WHOLE LIST: • IN[4]=% • OUT[4]={2.5,3,5.5}
  • 4. MANIPULATING ELEMENTS OF LISTS • Many of the most powerful list manipulation operations in the wolfram language treat whole lists as single objects. Sometimes, however, you need to pick out or set individual elements in a list. • You can refer to an element of a wolfram language list by giving its "index". The elements are numbered in order, starting at 1.
  • 5. • THIS EXTRACTS THE SECOND ELEMENT OF THE LIST: • IN[1]:= {5, 8, 6, 9}[[2]] • OUT[1]= 8 • THIS EXTRACTS A LIST OF ELEMENTS: • IN[2]:= {5, 8, 6, 9}[[{3, 1, 3, 2, 4}]] • OUT[2]= {6,5,6,8,9} • THIS ASSIGNS THE VALUE OF V TO BE A LIST: • IN[3]:= V = {2, 4, 7} • OUT[3]= {2,4,7} • YOU CAN EXTRACT ELEMENTS OF V: • IN[4]:= V[[2]] • OUT[4]= 4
  • 6. • By assigning a variable to be a list, you can use wolfram language lists much like "arrays" in other computer languages. Thus, for example, you can reset an element of a list by assigning a value to v[[i]]. • HERE IS A LIST: • IN[5]:= V = {4, -1, 8, 7} • OUT[5]= {4, -1, 8, 7} • THIS RESETS THE THIRD ELEMENT OF THE LIST: • IN[6]:= V[[3]] = 0 • OUT[6]=0 • NOW THE LIST ASSIGNED TO V HAS BEEN MODIFIED: • IN[7]:= V • OUT[7]= {4, -1, 0, 7}
  • 7. GETTING PIECES OF LISTS We will use this list for the examples: In[5]:= t = {a, b, c, d, e, f, g} Out[5]= {a, b, c, d, e, f, g} Here is the last element of t: In[2]:= Last[t] Out[2]= g This gives the third element: In[3]:= t[[3]] Out[3]= c This gives the list of elements 3 through 6: In[6]:= t[[3 ;; 6]] Out[6]= {c, d, e, f,} This gives a list of the first and fourth elements: In[4]:= t[[{1, 4}]] Out[4]= {a, d}
  • 8. • This gives the first three elements of the list t defined above: • t = {a, b, c, d, e, f, g} • In[5]:= Take[t, 3] • Out[5]= {a, b, c} • This gives the last three elements: • In[6]:= Take[t, -3] • Out[6]= {e, f, g} • This gives elements 2 through 5 inclusive: • In[7]:= Take[t, {2, 5}] • Out[7]= { b, c, d, e} • This gives t with the first element dropped: • In[9]:= Rest[t] • Out[9]= { b, c, d, e, f, g}
  • 9. • This Gives T With Its First Three Elements Dropped: • In[10]:= Drop[t, 3] • Out[10]= {d, e, f, g} • This Gives T With Only Its Third Element Dropped: • In[11]:= Drop[t, {3, 3}] • Out[11]= {a, b, d, e, f, g}
  • 10. TESTING AND SEARCHING LIST ELEMENTS • The wolfram system also has functions that search and test for elements of lists, based on the values of those elements. • This gives a list of the positions at which a appears in the list: • In[1]:= Position[{a, b, c, a, b}, a] • Out[1]= {{1},{4}} • Count counts the number of occurrences of a: • In[2]:= Count[{a, b, c, a, b}, a] • Out[2]= 2
  • 11. •This shows that a is an element of {a,b,c}: •In[3]:= MemberQ[{a, b, c}, a] •Out[3]= True •On the other hand, d is not: •In[4]:= MemberQ[{a, b, c}, d] •Out[4]= False
  • 12. ADDING, REMOVING, AND MODIFYING LIST ELEMENTS
  • 13. • This Gives A List With X Prepended: • In[1]:= Prepend[{a, b, c}, x] • Out[1]= {x,a,b,c} • This Inserts X So That It Becomes Element Number 2: • In[2]:= Insert[{a, b, c}, x, 2] • Out[2]= {a,x,b,c} • This Interleaves X Between The Entries Of The List: • In[1]:= Riffle[{a, b, c}, x] • Out[1]= {a,x,b,x,c} • This Replaces The Third Element In The List With X: • In[3]:= ReplacePart[{a, b, c, d}, 3 -> x] • Out[3]= {a,b,x,d}
  • 14. COMBINING LISTS • Join concatenates any number of lists together: • In[1]:= Join[{a, b, c}, {x, y}, {t, u}] • Out[1]={a,b,c,x,y,t,u} • Union combines lists, keeping only distinct elements: • In[2]:= Union[{a, b, c}, {c, a, d}, {a, d}] • Out[2]={a,b,c,d} • Riffle combines lists by interleaving their elements: • In[1]:= Riffle[{a, b, c}, {x, y, z}] • Out[1]={a,x,b,y,c,z}
  • 15. ORDERING IN LISTS • Here is a list of numbers: • In[50]:= t = {17, 21, 14, 9, 18} • Out[50]= {17, 21, 14, 9, 18} • This gives the elements of t in sorted order: • In[51]:= Sort[t] • Out[51]= {9,14,17,18,21} • This gives the positions of the elements of t, from the position of the smallest to that of the largest: • In[54]:= Ordering[t] • Out[54]= {4,3,1,5,2} • This is the same as Sort[t]: • In[55]:= t[[%]] • Out[55]= {9,14,17,18,21} • This gives the smallest element in the list: • In[2]:= Min[t] • Out[2]= 9
  • 16. • Plot The List Of Numbers {1, 1, 2, 2, 3, 4, 4}: • In[2]:= ListPlot[{1, 1, 2, 2, 3, 4, 4}] • Plot the list of numbers {10, 9, 8, 7, 3, 2, 1}: • In[3]:= ListPlot[{10, 9, 8, 7, 3, 2, 1}] In the list plot : Vertical (y)= {1,1,2,2,3,4,4} Horizontal (x)= {1,2,3,4,5,6,7} list is indexes
  • 17. • Generate a list of numbers, then plot it: • In[5]:=ListPlot[Range[20]] Reverse the elements in a list: In[6]:= Reverse[{1, 2, 3, 4}] In[6]:={4,3,2,1}
  • 18. EXERCISES • Reverse[Range[10]] • Make a list of numbers up to 100. • Make a list of numbers from 1 to 50 in reverse order
  • 20. Making Tables  Table makes a list with a single element repeated some specified number of times.  Make a list that consists of 5 repeated 10 times:  In[1]:= Table[5, 10]  Out[1]= {5,5,5,5,5,5,5,5,5,5}  This makes a list with x repeated 10 times:  In[2]:= Table[x, 10]  Out[2]= {x,x,x,x,x,x,x,x,x,x}  You can repeat lists too:  In[3]:= Table[{1, 2}, 10]  Out[3]= { {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2}, {1,2} }
  • 21.  Make a table that gives the value of n+1 when n goes from 1 to 10:  In[6]:= Table[n + 1, {n, 10}]  Out[6]= {2,3,4,5,6,7,8,9,10,11}  Make a table of the first 10 squares:  In[7]:= Table[n^2, {n, 10}]  Out[7]= {1,4,9,16,25,36,49,64,81,100}  Here’s a table of successively longer lists produced by Range:  In[8]:= Table[Range[n], {n, 5}]  Out[8]= {{1},{1,2},{1,2,3},{1,2,3,4,},{1,2,3,4,5}}  In[9]=TableForm[%]  Out[9]=  1  1 2  1 2 3  1 2 3 4  1 2 3 4 5
  • 22.  Here each of the lists produced is shown as a column:  In[9]:= Table[Column[Range[n]], {n, 8}] This generates a table with n going from 4 to 10: In[15]:= Table[f[n], {n, 4, 10}] Out[15]={f[4],f[5], f[6], f[7], f[8], f[9], f[10],}} This makes n go from 4 to 10 in steps of 2:  In[16]:= Table[f[n], {n, 4, 10, 2}]  Out[16]={f[4],f[6], f[8], f[10],}  This generates 20 independent random integers with size up to 10:  In[22]:= Table[RandomInteger[10], 20]  Out[22]={3,1,4,3,6,7,6,10,9,2,1,4,5,8,3,8,3,8,3,0}
  • 23.  The Wolfram Language emphasizes consistency, so for example Range is set up to deal with starting points and steps just like Table.  Generate the range of numbers 4 to 10:  In[17]:= Range[4, 10]  Out[17]={4,5,6,7,8,9,10}  Generate the range of numbers 4 to 10 going in steps of 2:  In[18]:= Range[4, 10, 2]  Out[18]={4,6,8,10}
  • 25. Exercises 1) Make a list in which the number 1000 is repeated 5 times. 2) This makes a list with x repeated 7 times: 3) This makes n go from 4 to 20 in steps of 2: 4) Make a table of the first 8 squares: 5) Table[Range[n], {n, 10}] 6) TableForm[%] 7) Table[Column[Range[n]], {n, 4,10,2}] 8) Table[PieChart[Table[1,n]],{n,3}]
  • 26. Vector Operations  This is a vector in three dimensions:  In[14]:= v = {1, 3, 2}  Out[14]={1, 3, 2}  This gives a vector u in the direction opposite to v with twice the magnitude:  In[15]:= u = -2 v  Out[15]={-2, -6, -4}
  • 28. Multiplying Vectors  This multiplies each element of the vector by the scalar k:  In[1]:= k {a, b, c}  Out[1]= {ak, bk, ck}  The "dot" operator gives the scalar product of two vectors:  In[2]:= {a, b, c} . {ap, bp, cp}  Out[2]= aap, bbp, ccp  You can also use dot to multiply a matrix by a vector:  In[3]:= {{a, b}, {c, d}} . {x, y}  Out[3]= {ax+by,cx+dy}  Dot is also the notation for matrix multiplication in the Wolfram Language:  In[4]:= {{a, b}, {c, d}} . {{1, 2}, {3, 4}}  Out[4]= {{a+3b,2a+4b},{c+3d,2c+4d}}
  • 29.  Here are definitions for a matrix m and a vector v:  In[5]:= m = {{a, b}, {c, d}}; v = {x, y}  Out[5]= {x, y}  This left‐multiplies the vector v by m. The object v is effectively treated as a column vector in this case:  In[6]:= m . v  Out[6]= {ax+by,cx+dy}  You can also use dot to right‐multiply v by m. Now v is effectively treated as a row vector:  In[7]:= v . m  Out[7]= {ax+cy,bx+dy}  You can multiply m by v on both sides to get a scalar:  In[8]:= v . m . v  Out[8]= x(ax+cy)+y,(bx+dy)
  • 30. Matrix  MatrixForm formats arrays using standard matrix formatting:  In[1]= m = {{a, b, c}, {d, e, f}};  In[2]= MatrixForm[m]  When an input evaluates to MatrixForm[expr], MatrixForm does not appear in the output:  In[1]:= MatrixForm[{{1, 2}, {3, 4}}]  This creates a matrix that only has nonzero entries on the diagonal:  In[2]:= DiagonalMatrix[{1, 2, 3, 4}] // MatrixForm
  • 36. SETS
  • 38. Union  Give a sorted list of distinct elements:  In[1]:= Union[{1, 2, 1, 3, 6, 2, 2}]  Out[1]= {1,2,3,6}  Give a sorted list of distinct elements from all the lists:  In[1]:= Union[{a, b, a, c}, {d, a, e, b}, {c, a}]  Out[1]= {a,b,c,d,e}  Give a list of the distinct lists:  In[1]:= Union[{{1, 2}, {1, 2, 3}}, {{2, 1}, {1, 2}}, {{3, 2, 1}, {1, 2, 3}}]  Out[1]= {{1, 2}, {2, 1}, {1, 2, 3},{3, 2, 1}}  Enter using un:  In[1]:= {a, b, c} Ս {b, c, d}  Out[1]= {a,b,c,d}
  • 39. Intersection & Complement  Find elements common to all the lists given:  In[1]:= Intersection[{1, 1, 2, 3}, {3, 1, 4}, {4, 1, 3, 3}]  Out[1]= {1,3}  Enter using inter:  In[1]:= {a, b, c} Ո {b, c, d}  Out[1]= {b,c}  Find which elements in the first list are not in any of the subsequent lists:  In[1]:= Complement[{a, b, c, d, e}, {a, c}, {d}]  Out[1]= {b,e}  The result of Complement[list1,list2] is sorted and does not contain repeated elements:  In[1]:= Complement[{b, e, d, a, b, c, d}, {b, c}]  Out[1]= {a,d,e}
  • 40. Subsets  This gives all the subsets of the list:  In[4]:= Subsets[{a, b, c}]  Out[4]= {{},{a},{b},{c},{a,b},{a,c}.{b,c},{a,b,c}}  Delete Duplicates deletes all duplicate elements from the list:  In[5]:= DeleteDuplicates[{a, b, c, a}]  Out[5]= {a,b,c}  All possible subsets (power set):  In[6]:= Subsets[{a, b, c}]  Out[6]={{}, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}
  • 41. All possible subsets containing up to 2 elements:  In[7]:= Subsets[{a, b, c, d}, 2]  Out[7]={{}, {a}, {b}, {c}, {d}, {a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d}} Subsets containing exactly 2 elements:  In[8]:= Subsets[{a, b, c, d}, {2}]  Out[8]={{a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d}} The first 5 subsets containing 3 elements:  In[9]:= Subsets[{a, b, c, d, e}, {3}, 5]  Out[9]={{a, b, c}, {a, b, d}, {a, b, e}, {a, c, d}, {a, c, e}}

Editor's Notes

  • #18: https://www.wolfram.com/language/elementary-introduction/3rd-ed/03-first-look-at-lists.html