Debian Codegraph @

docs/manual/Scripting_5_7__Vectors_and_matrices.html

raw
1 <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
2 <title>Scripting 5.7. Vectors and matrices</title>
3 <style>
4 td { padding-left: 5pt; padding-right: 5pt; }
5 th { padding-left: 5pt; padding-right: 5pt; }
6 code { white-space: pre-wrap; }
7 dd { white-space: pre-wrap; }
8 </style>
9 </head><body bgcolor="#FFFFFF">
10
11 <table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b>
12 Scripting 5.7. Vectors and matrices
13 </b></font></table></table>
14 <h2>1. What is a vector?</h2>
15 <p>A <b>numeric vector</b> is an array of numbers, regarded as a single object. For instance, the squares of the first five integers can be collected in the vector { 1, 4, 9, 16, 25 }. In a Praat script, you can put a vector into a variable whose name ends in a number sign (&#8220;<code><font size=+1>#</font></code>&#8221;):</p>
16 <code> squares# = { 1, 4, 9, 16, 25 }<br></code>
17 <p>After this, the variable <i><code><font size=+1>squares#</font></code></i> contains the value { 1, 4, 9, 16, 25 }. We say that the vector <i><code><font size=+1>squares#</font></code></i> has five <i>dimensions</i>, i.e. it contains five numbers.</p>
18 <p>Whereas in <a href="Scripting_3_2__Numeric_variables.html">Scripting 3.2. Numeric variables</a> we talked about a numeric variable as being analogous to a house where somebody (the numeric <i>value</i>) could live, a numeric vector with five dimensions can be seen as a <i>street</i> that contains five houses, which are numbered with the indexes 1, 2, 3, 4 and 5, each house containing a numeric value. Thus, the street <i><code><font size=+1>squares#</font></code></i> contains the following five houses: <i><code><font size=+1>squares#</font></code></i> [1], <i><code><font size=+1>squares#</font></code></i> [2], <i><code><font size=+1>squares#</font></code></i> [3], <i><code><font size=+1>squares#</font></code></i> [4] and <i><code><font size=+1>squares#</font></code></i> [5]. Their values (the numbers that currently live in these houses) are 1, 4, 9, 16 and 25, respectively.</p>
19 <p>To list the five values with a loop, you could do:</p>
20 <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: “Some squares:”<br></code>
21 <code> <b>for</b> i <b>from</b> 1 <b>to</b> size (squares#)<br></code>
22 <code> <a href="_appendInfoLine_.html">appendInfoLine</a>: “The square of ”, i, “ is ”, squares# [i]<br></code>
23 <code> <b>endfor</b><br></code>
24 <code style="color:red">=&gt;</code><br>
25 <code> Some squares:<br></code>
26 <code> The square of 1 is 1<br></code>
27 <code> The square of 2 is 4<br></code>
28 <code> The square of 3 is 9<br></code>
29 <code> The square of 4 is 16<br></code>
30 <code> The square of 5 is 25<br></code>
31 <p>Instead of the above procedure to get the vector <i><code><font size=+1>squares#</font></code></i>, with a pre-computed list of five squares, you could compute the five values with a formula, as in the example of <a href="Scripting_5_6__Arrays_and_dictionaries.html">Scripting 5.6. Arrays and dictionaries</a>. However, in order to put a value into an element of the vector, you have to create the vector first (i.e., you have to build the whole street before you can put something in a house), so we start by creating a vector with five zeroes in it:</p>
32 <code> squares# = zero# (5)<br></code>
33 <p>After this, <i><code><font size=+1>squares#</font></code></i> is the vector { 0, 0, 0, 0, 0 }, i.e., the value of each element is zero. Now that the vector (street) exists, we can put values into (populate) the five elements (houses):</p>
34 <code> <b>for</b> i <b>from</b> 1 <b>to</b> size (squares#)<br></code>
35 <code> squares# [i] = i * i<br></code>
36 <code> <b>endfor</b><br></code>
37 <p>After this, the variable <i><code><font size=+1>squares#</font></code></i> has the value { 1, 4, 9, 16, 25 }, as before, but now we had the computer compute the squares.</p>
38 <h2>2. Creating a vector</h2>
39 <p>You can create a vector in many ways. The first way we saw was with a <b>vector literal</b>, i.e. a series of numbers (or numeric formulas) between braces:</p>
40 <code> lengths# = { 1.83, 1.795, 1.76 }<br></code>
41 <p>The second way we saw was to create a series of <b>zeroes</b>. To create a vector consisting of 10,000 zeroes, you do</p>
42 <code> longEmptyArray# = <b><a href="_zero-H_.html">zero#</a></b> (10000)<br></code>
43 <p>Another important type of vector is a series of random numbers. To create a vector consisting of 10,000 values drawn from a <b>Gaussian distribution</b> with true mean 0.0 and true standard deviation 1.0, you could do</p>
44 <code> noise# = <b><a href="_randomGauss-H_.html">randomGauss#</a></b> (10000, 0.0, 1.0)<br></code>
45 <p>To create a vector consisting of 10,000 values drawn from a <b>uniform distribution of real numbers</b> with true minimum 0.0 and true maximum 1.0, you use</p>
46 <code> a# = <b><a href="_randomUniform-H_.html">randomUniform#</a></b> (10000, 0.0, 1.0)<br></code>
47 <p>To create a vector consisting of 10,000 values drawn from a <b>uniform distribution of integer numbers</b> with true minimum 1 and true maximum 10, you use</p>
48 <code> a# = <b><a href="_randomInteger-H_.html">randomInteger#</a></b> (10000, 1, 10)<br></code>
49 <p>To create a vector containing the integer numbers 1 through 64, you use</p>
50 <code> a# = <b><a href="_to-H_.html">to#</a></b> (64)<br></code>
51 <p>To create a vector containing the integer numbers 10 through 20, you use</p>
52 <code> a# = <b><a href="_from_to-H_.html">from_to#</a></b> (10, 20)<br></code>
53 <p>To create a vector containing linearly increasing (not necessarily integer) numbers from 10 through 20 in steps of 2, you use</p>
54 <code> a# = <b><a href="_from_to_by-H_.html">from_to_by#</a></b> (10, 20, 2)<br></code>
55 <p>To create five linearly increasing numbers between 0 and 10 (i.e. { 0, 2.5, 5, 7.5, 10 }), you use</p>
56 <code> a# = <b><a href="_from_to_count-H_.html">from_to_count#</a></b> (0, 10, 5)<br></code>
57 <p>To divide the range between 0 and 12 symmetrically with step 5 (i.e. { 1, 6, 11 }), you use</p>
58 <code> a# = <b><a href="_between_by-H_.html">between_by#</a></b> (0, 12, 5)<br></code>
59 <p>To divide the range between 0 and 10 into five equal parts and list their centres (i.e. { 1, 3, 5, 7, 9 }), you use</p>
60 <code> a# = <b><a href="_between_count-H_.html">between_count#</a></b> (0, 10, 5)<br></code>
61 <p>To sort the numbers in a vector (e.g. { 7.4, 1.3, 3.6 }), you use</p>
62 <code> a# = <b><a href="_sort-H_.html">sort#</a></b> ({ 7.4, 1.3, 3.6 })<br></code>
63 <code> <a href="_writeInfoLine_.html">writeInfoLine</a> (a#)<br></code>
64 <code style="color:red">=&gt;</code><br>
65 <code> 1.3 3.6 7.4<br></code>
66 <p>To randomly shuffle the numbers in a vector (e.g. { 7.4, 1.3, 3.6 }), you use</p>
67 <code> a# = <b><a href="_shuffle-H_.html">shuffle#</a></b> ({ 7.4, 1.3, 3.6 })<br></code>
68 <p>which can yield { 1.3, 7.4, 3.6 } or any of the five other orders of the elements.</p>
69 <p>Vectors can also be created by some menu commands. For instance, to get vectors representing the times and pitch frequencies of the frames in a Pitch object, you can do</p>
70 <code> <a href="_selectObject_.html">selectObject</a>: myPitch<br></code>
71 <code> times# = List all frame times<br></code>
72 <code> pitches# = List values in all frames: “hertz”<br></code>
73 <h2>3. Turning a vector into a number</h2>
74 <p>For the vector defined above, you can compute the <b>sum</b> of the five values as</p>
75 <code> <b><a href="_sum_.html">sum</a></b> (squares#)<br></code>
76 <p>which gives 55. You compute the <b>average</b> of the five values as</p>
77 <code> <b><a href="_mean_.html">mean</a></b> (squares#)<br></code>
78 <p>which gives 11. You compute the <b>standard deviation</b> of the values as </p>
79 <code> <b><a href="_stdev_.html">stdev</a></b> (squares#)<br></code>
80 <p>which gives 9.669539802906858 (the standard deviation is undefined for vectors with fewer than 2 elements). The <b>center of gravity</b> of the distribution defined by regarding the five values as relative frequencies as a function of the index from 1 to 5 is computed by</p>
81 <code> <b><a href="_center_.html">center</a></b> (squares#)<br></code>
82 <p>which gives 4.090909090909091 (for a vector with five elements, the result will always be a number between 1.0 and 5.0). You compute the <b>inner product</b> of two equally long vectors as follows:</p>
83 <code> other# = { 2, 1.5, 1, 0.5, 0 }<br></code>
84 <code> result = <b><a href="_inner_.html">inner</a></b> (squares#, other#)<br></code>
85 <p>which gives 1*2 + 4*1.5 + 9*1 + 16*0.5 + 25*0 = 25. The formula for this is &#8721;<sub><i>i</i>=1</sub><sup>5</sup> <i>squares</i>[<i>i</i>] * <i>other</i>[<i>i</i>], so that an alternative piece of code could be</p>
86 <code> result = <b><a href="_sumOver_.html">sumOver</a></b> (i to 5, squares# [i] * other# [i])<br></code>
87 <h2>4. Converting vectors to vectors</h2>
88 <code> a# = squares# + 5 ; adding a number to each element of a vector<br></code>
89 <code> assert a# = { 6, 9, 14, 21, 30 }<br></code>
90 <p>causes <i><code><font size=+1>a#</font></code></i> to become the vector { 6, 9, 14, 21, 30 }.</p>
91 <code> b# = a# + { 3.14, 2.72, 3.16, -1, 7.5 } ; adding two vectors of the same length<br></code>
92 <p>causes <i><code><font size=+1>b#</font></code></i> to become the vector { 9.14, 11.72, 17.16, 20, 37.5 }.</p>
93 <code> c# = b# / 2 ; dividing each element of a vector<br></code>
94 <p>causes <i><code><font size=+1>c#</font></code></i> to become the vector { 4.57, 5.86, 8.58, 10, 18.75 }.</p>
95 <code> d# = b# * c# ; elementwise multiplication<br></code>
96 <p>causes <i><code><font size=+1>d#</font></code></i> to become the vector { 41.7698, 68.6792, 147.2328, 200, 703.125 }.</p>
97 <p>A vector can also be given to a <b>menu command</b> that returns another vector. For instance, to get a vector representing the pitch frequencies at 0.01-second intervals in a Pitch object, you can do</p>
98 <code> <a href="_selectObject_.html">selectObject</a>: myPitch<br></code>
99 <code> tmin = Get start time<br></code>
100 <code> tmax = Get end time<br></code>
101 <code> times# = <b><a href="_between_by-H_.html">between_by#</a></b> (tmin, tmax, 0.01)<br></code>
102 <code> pitches# = List values at times: times#, “hertz”, “linear”<br></code>
103 <h2>5. What is a matrix?</h2>
104 <p>A <b>numeric matrix</b> is a two-indexed array of numbers, regarded as a single object. In a Praat script, you can put a matrix into a variable whose name ends in two number signs (&#8220;<code><font size=+1>##</font></code>&#8221;):</p>
105 <code> confusion## = {{ 3, 6, 2 }, { 8, 2, 1 }}<br></code>
106 <p>After this, the variable <i><code><font size=+1>confusion##</font></code></i> contains the value {{ 3, 6, 2 }, { 8, 2, 1 }}. We say that the matrix <i><code><font size=+1>confusion##</font></code></i> has two <i>rows</i> and three <i>columns</i>, i.e. it contains six numbers.</p>
107 <p>Whereas a numeric vector with five dimensions could be seen (see above) as a street that contains five houses, the matrix <i><code><font size=+1>confusion##</font></code></i> can be seen as a city district with two avenues crossed by three streets, where everybody lives on an intersection (the analogies start to get less realistic).</p>
108 <h2>6. Creating a matrix</h2>
109 <p>You can create a matrix in many ways. The first way we saw was with a <b>matrix literal</b>, i.e. a series of series of numbers (or numeric formulas) between nested braces.</p>
110 <p>The second way is as a matrix of <b>zeroes</b>. To create a matrix consisting of 100 rows of 10,000 zeroes, you do</p>
111 <code> a## = <b><a href="_zero-H-H_.html">zero##</a></b> (100, 10000)<br></code>
112 <p>After this,</p>
113 <code> <b><a href="_numberOfRows_.html">numberOfRows</a></b> (a##)<br></code>
114 <p>is 100, and</p>
115 <code> <b><a href="_numberOfColumns_.html">numberOfColumns</a></b> (a##)<br></code>
116 <p>is 10000.</p>
117 <p>Another important type of matrix is one filled with random numbers. To create a matrix consisting of 100 rows of 10,000 values drawn from a <b>Gaussian distribution</b> with true mean 0.0 and true standard deviation 1.0, you can do</p>
118 <code> noise## = <b><a href="_randomGauss-H-H_.html">randomGauss##</a></b> (100, 10000, 0.0, 1.0)<br></code>
119 <p>You can create a matrix as the outer product of two vectors:</p>
120 <code> m## = <b><a href="_outer-H-H_.html">outer##</a></b> (u#, v#)<br></code>
121 <p>which is the same as</p>
122 <code> m## = <b><a href="_zero-H-H_.html">zero##</a></b> (<a href="_size_.html">size</a> (u#), <a href="_size_.html">size</a> (v#))<br></code>
123 <code> <b>for</b> irow <b>to</b> <a href="_size_.html">size</a> (u#)<br></code>
124 <code> <b>for</b> icol <b>to</b> <a href="_size_.html">size</a> (v#)<br></code>
125 <code> m## [irow, icol] = u# [irow] * v# [icol]<br></code>
126 <code> <b>endfor</b><br></code>
127 <code> <b>endfor</b><br></code>
128 <p>or in mathematical notation</p>
129 <table width="100%" style="white-space:pre-wrap"><tr><td align=middle><i>m</i><sub><i>ij</i></sub> = <i>u</i><sub><i>i</i></sub> <i>v</i><sub><i>j</i></sub> (<i>i</i> = 1..<i>M</i>, <i>j</i> = 1..<i>N</i>)</table>
130 <p>where <i>M</i> is the number of rows and <i>N</i> is the number of columns.</p>
131 <h2>7. Computations with matrices</h2>
132 <p>You can add matrices:</p>
133 <code> c## = a## + b##<br></code>
134 <p>Elementwise multiplication:</p>
135 <code> c## = a## * b##<br></code>
136 <p>which does</p>
137 <table width="100%" style="white-space:pre-wrap"><tr><td align=middle><i>c</i><sub><i>ij</i></sub> = <i>a</i><sub><i>ij</i></sub> <i>b</i><sub><i>ij</i></sub> (<i>i</i> = 1..<i>M</i>, <i>j</i> = 1..<i>N</i>)</table>
138 <p>Matrix multiplication:</p>
139 <code> c## = mul## (a##, b##)<br></code>
140 <p>which does</p>
141 <table width="100%" style="white-space:pre-wrap"><tr><td align=middle><i>m</i><sub><i>ij</i></sub> = u<sub><i>k</i>=1</sub><sup>K</sup> <i>a</i><sub><i>ik</i></sub> <i>b</i><sub><i>kj</i></sub> (<i>i</i> = 1..<i>M</i>, <i>j</i> = 1..<i>N</i>)</table>
142 <p>where <i>M</i> is the number of rows of <i>a</i>, <i>N</i> is the number of columns of <i>b</i>, and <i>K</i> is the number of columns of <i>a</i>, which has to be equal to the number if rows of <i>b</i>.</p>
143 <p>Matrix-by-vector multiplication:</p>
144 <code> v# = mul# (m##, u#)<br></code>
145 <p>which does</p>
146 <table width="100%" style="white-space:pre-wrap"><tr><td align=middle><i>v</i><sub><i>i</i></sub> = u<sub><i>j</i>=1</sub><sup>N</sup> <i>m</i><sub><i>ij</i></sub> <i>u</i><sub><i>j</i></sub> (<i>i</i> = 1..<i>M</i>)</table>
147 <p>where <i>M</i> is the number of rows of <i>m</i>, and <i>N</i> is the number of columns of <i>m</i>, which has to be equal to the dimension of <i>u</i>. Also</p>
148 <code> v# = mul# (u#, m##)<br></code>
149 <p>which does</p>
150 <table width="100%" style="white-space:pre-wrap"><tr><td align=middle><i>v</i><sub><i>j</i></sub> = u<sub><i>i</i>=1</sub><sup>M</sup> <i>u</i><sub><i>i</i></sub> <i>m</i><sub><i>ij</i></sub> (<i>j</i> = 1..<i>N</i>)</table>
151 <p>where <i>M</i> is the number of rows of <i>m</i>, which has to be equal to the dimension of <i>u</i>, and <i>N</i> is the number of columns of <i>m</i>.</p>
152 <h2>8. String vectors</h2>
153 <p>You can create string vectors (also called string arrays) in the following ways:</p>
154 <code> a$# = { “hello”, “goodbye” }<br></code>
155 <p>creates a vector with two strings, which you can access as <code><font size=+1>a$# [1]</font></code>, which is &#8220;hello&#8221;, and <code><font size=+1>a$# [2]</font></code>, which is &#8220;goodbye&#8221;.</p>
156 <code> a$# = <b><a href="_empty-S-H_.html">empty$#</a></b> (10)<br></code>
157 <p>creates a vector with 10 empty strings, which you can access as <code><font size=+1>a$# [1]</font></code> through <code><font size=+1>a$# [10]</font></code>.</p>
158 <code> text$# = <b><a href="_readLinesFromFile-S-H_.html">readLinesFromFile$#</a></b> (“hello.txt”)<br></code>
159 <p>creates a vector with 100 strings if the file <code><font size=+1>hello.text</font></code> contains 100 lines of text.</p>
160 <code> fileNames$# = <b><a href="_fileNames-S-H_.html">fileNames$#</a></b> (“sound/*.wav”)<br></code>
161 <p>creates a vector containing the names of all WAV files in the folder <code><font size=+1>sound</font></code>.</p>
162 <code> folderNames$# = <b><a href="_folderNames-S-H_.html">folderNames$#</a></b> (“.”)<br></code>
163 <p>creates a vector containing the names of all folders in the folder where the script resides.</p>
164 <code> inks$# = <b><a href="_splitByWhitespace-S-H_.html">splitByWhitespace$#</a></b> (“Hello, how are you?”)<br></code>
165 <p>creates a vector containing the strings &#8220;Hello,&#8221; (including the comma), &#8220;how&#8221;, &#8220;are&#8221;, and &#8220;you?&#8221;.</p>
166 <h3>Links to this page</h3>
167 <ul>
168 <li><a href="Formulas_5__Mathematical_functions.html">Formulas 5. Mathematical functions</a>
169 <li><a href="Scripting.html">Scripting</a>
170 <li><a href="Scripting_5__Language_elements_reference.html">Scripting 5. Language elements reference</a>
171 <li><a href="What_was_new_in_6_1_.html">What was new in 6.1?</a>
172 </ul>
173 <hr>
174 <address>
175 <p>© Paul Boersma 2023-01-30</p>
176 </address>
177 </body>
178 </html>