<< Chapter < Page | Chapter >> Page > |
There are four basic arithmetic operators in .m files:
+ addition
- subtraction
* multiplication
/ division (for matrices, it also means inversion)
The following three operators work on an element-by-element basis:
.* multiplication of two vectors, element-wise
./ division of two vectors, element-wise
.^ raising all the elements of a vector to a power
As an example, to evaluate the expression ${a}^{3}+\sqrt{\text{bd}}-\mathrm{4c}$ , where $a=1\text{.}2$ , $b=2\text{.}3$ , $c=4\text{.}5$ and $d=4$ , type the following commands in the Command Window to get the answer (ans) :
>>a=1.2;
>>b=2.3;
>>c=4.5;
>>d=4;
>>a^3+sqrt(b*d)-4*c
ans =
-13.2388
Note the semicolon after each variable assignment. If the semicolon is omitted, the interpreter echoes back the variable value.
Consider the vectors x $=[{x}_{1},{x}_{2},\text{.}\text{.}\text{.},{x}_{n}]$ and y $=[{y}_{1},{y}_{2},\text{.}\text{.}\text{.},{y}_{n}]$ . The following operations indicate the resulting vectors:
x*.y $=[{x}_{1}{y}_{1},{x}_{2}{y}_{2},\text{.}\text{.}\text{.},{x}_{n}{y}_{n}]$
x./y $=\left[\frac{{x}_{1}}{{y}_{1}},\frac{{x}_{2}}{{y}_{3}},\text{.}\text{.}\text{.},\frac{{x}_{n}}{{y}_{n}}\right]$
x.^p $=\left[{x}_{1}^{p},{x}_{2}^{p},\text{.}\text{.}\text{.},{x}_{n}^{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\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, $\sum _{i}{x}_{i}{y}_{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\times \mathrm{4,3}\times \mathrm{5,4}\times 6]$ = $[\begin{array}{ccc}4& \text{15}& \text{24}]\end{array}$ .
LabVIEW MathScript supports complex numbers. The imaginary number is denoted with the symbol i or j, assuming that these symbols have not been used any other place in the program. It is critical to avoid such a symbol conflict for obtaining correct outcome. Enter the following and observe the outcomes:
>>z=3 + 4i % note the multiplication sign ‘*’ is not needed after 4
>>conj(z) % computes the conjugate of z
>>angle(z) % computes the phase of z
>>real(z) % computes the real part of z
>>imag(z) % computes the imaginary part of z
>>abs(z) % computes the magnitude of z
One can also define an imaginary number with any other user-specified variables. For example, try the following:
>>img=sqrt(-1)
>>z=3+4*img
>>exp(pi*img)
In .m files, all arrays (vectors) are indexed starting from 1 − in other words, x(1) denotes the first element of the array x. Note that the arrays are indexed using parentheses (.) and not square brackets [.], as done in C/C++. To create an array featuring the integers 1 through 6 as elements, enter:
Notification Switch
Would you like to follow the 'An interactive approach to signals and systems laboratory' conversation and receive update notifications?