Consider the vectors x
=[x1,x2,...,xn]=[x1,x2,...,xn] size 12{ {}= \[ x rSub { size 8{1} } ,x rSub { size 8{2} } , "." "." "." ,x rSub { size 8{n} } \] } {}and y
=[y1,y2,...,yn]=[y1,y2,...,yn] size 12{ {}= \[ y rSub { size 8{1} } ,y rSub { size 8{2} } , "." "." "." ,y rSub { size 8{n} } \] } {}. The following operations indicate the resulting vectors:
x*.y
=[x1y1,x2y2,...,xnyn]=[x1y1,x2y2,...,xnyn] size 12{ {}= \[ x rSub { size 8{1} } y rSub { size 8{1} } ,x rSub { size 8{2} } y rSub { size 8{2} } , "." "." "." ,x rSub { size 8{n} } y rSub { size 8{n} } \] } {}
x./y
=x1y1,x2y3,...,xnyn=x1y1,x2y3,...,xnyn size 12{ {}= left [ { {x rSub { size 8{1} } } over {y rSub { size 8{1} } } } , { {x rSub { size 8{2} } } over {y rSub { size 8{3} } } } , "." "." "." , { {x rSub { size 8{n} } } over {y rSub { size 8{n} } } } right ]} {}
x.^p
=x1p,x2p,...,xnp=x1p,x2p,...,xnp size 12{ {}= left [x rSub { size 8{1} } rSup { size 8{p} } ,x rSub { size 8{2} } rSup { size 8{p} } , "." "." "." ,x rSub { size 8{n} } rSup { size 8{p} } right ]} {}
Note that because the boldfacing of vectors/matrices are not used in .m files, in the notation adopted in this book, no boldfacing of vectors/matrices is shown to retain consistency with .m files.
The arithmetic operators + and – can be used to add or subtract matrices, vectors or scalars. Vectors denote one-dimensional arrays and matrices denote multidimensional arrays. For example,
>> x=[1,3,4]
>> y=[4,5,6]
>> x+y
ans=
5 8 10
In this example, the operator + adds the elements of the vectors x and y, element by element, assuming that the two vectors have the same dimension, in this case
1×31×3 size 12{1 times 3} {} or one row with three columns. An error occurs if one attempts to add vectors having different dimensions. The same applies for matrices.
To compute the dot product of two vectors (in other words,
∑ixiyi∑ixiyi size 12{ Sum cSub { size 8{i} } {x rSub { size 8{i} } y rSub { size 8{i} } } } {} ), use the multiplication operator ‘*’ as follows:
>> x*y'
ans =
43
Note the single quote after y denotes the transpose of a vector or a matrix.
To compute an element-by-element multiplication of two vectors (or two arrays), use the following operator:
>> x .* y
ans =
4 15 24
That is, x .* y means
[1×4,3×5,4×6][1×4,3×5,4×6] size 12{ \[ 1 times 4,3 times 5,4 times 6 \] } {} =
[41524][41524] size 12{ \[ matrix {
4 {} # "15" {} # "24" \] {}
} } {}.