LAB 02: MATLAB Programming: 2.1 Basic Symbols and Commands
LAB 02: MATLAB Programming: 2.1 Basic Symbols and Commands
2.1
Matrix: The basic computational unit in MATLAB is the rectangular matrix with real or
complex elements. Square dimensional matrices, vectors and scalar variables are considered
particular cases of this basic data structure.
Matrix input: Matrix elements are introduced inside brackets, [ ]. Elements from the same
row can be separated either by a space or by a comma. A different row is specified by the use
of semicolons or by means the enter key .
Submatrix reference: Given a matrix A, if we want to pick out the sub-matrix B defined as
the rows 3 to 5 and the columns 4 to 7 of matrix A, two possibilities are:
B=A(3:5,4:7)
or
B=A([3,4,5],[4 5 6 7])
To obtain sub-matrix C defined by the rows 1, 3 and 4 of A, the command is:
C=A([1 3 4],:)
The comma separates the specification of rows from the specification of columns. Notice that
the two points symbol, :, after the comma means all columns.
Special symbols: There exist several predefined variables. Imaginary number 1 can be
expressed either as i or j, Inf corresponds to + and pi refers to number. NaN (Not-aNumber) is obtained in non-definite operations, such as 0/0.
Learning MATLAB
Page 1 of 6
Page 2 of 6
Polynomials: Polynomials are entered as row vectors where the elements are the polynomial
coefficients. For instance, polynomial s 4 + 5s 2 + 3s 10 is introduced as:
polinomial=[1 0 5 3 -10];
Notice that the coefficient corresponding to s3 is zero.
Polynomial product: To multiply polynomials, you can use the convolution product function,
onv. It only allows two input arguments at a time but it is possible to nest the functions. To
get the product (s + 2) (s2 + 4s) (3s2 + 7s + 2) type the following command:
Learning MATLAB
Page 3 of 6
To organise different information types by means several fields you can use struct type
variables. Type >>help struct for more information. For instance, if you want to save the
input and output records of an experiment along with the realisation date, you can type:
Format: The command format allows changing the numerical format in which MATLAB
presents the results. Several formats are:
short: fixed comma with 4 decimals (it is the default)
long: fixed comma with 15 decimals
bank: two decimals
rational: to show the rational numbers as the ratio of two integers.
2.2
Learning MATLAB
Page 4 of 6
Point before a mathematical operator: To put a point before an operator between two vectors
or matrices indicates that the operation must be performed element-to-element. For instance,
A*B is the matrix product of matrices A and B. If A and B have the same dimension, typing
C=A.*B, results in a C matrix which elements cij are computed as cij=aijbij.
2.3
Logical operators
Relational operators are == (equality), <, <= (less than, less or equal than) y >, >= (greater,
than, greater or equal than). There also exist commands for the Boolean comparisons, e.g.,
gt(i,1) means i greater than 1.
Boolean conditions are expressed inside parentheses, for instance, (a==2). If this condition is
true, the output value is 1, if it is false, the output value is 0.
Logical operators are & (and), | (or) y ~ (not); xor is not an operator but a function.
2.4
Learning MATLAB
Page 5 of 6
Learning MATLAB
Page 6 of 6