<< 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 , where , , and , 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 and y . The following operations indicate the resulting vectors:
x*.y
x./y
x.^p
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 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, ), 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 = .
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?