- In Matlab, one manipulates vectors or matrices of raw numbers.
- Elements of a matrix should be separated by spaces and/or commas and specified within brackets ([ ]). Rows are separated by semicolons.
>> [ 1 2 3; 4 5 6 ]
ans =
1 2 3
4 5 6
- We will often work with row or column vectors:
>> x = [ 1 2 3 4 5 6 ]
X =
1 2 3 4 5 6
- The previously defined row vector can be transposed to a column vector using either the transpose() function or the .' operator:
>> x.'
ans =
1
2
3
4
5
6
- The colon (:) operator is especially useful in Matlab. It can be used to create vectors of regularly spaced values as follows:
>> 2 : 0.2 : 4
ans =
2.00 2.20 2.40 2.60 2.80 3.00 3.20 3.40 3.60 3.80 4.00
- The increment value can be omitted, in which case it defaults to 1:
>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
- As well, the colon operator can be used to select an entire row or column of a matrix:
>> x = [ 1 2 3; 4 5 6 ]
x =
1 2 3
4 5 6
>> x(:, 1)
ans =
1
4
- The multiplication operator (*) implies matrix multiplication, which is only possible when the matrices being multiplied have compatible dimensions (i.e., the number of columns of the first matrix is equal to the number of rows of the second):
>> [1 2 3] * [4 5 6].'
ans =
32
- Pointwise operations on arrays, such as squaring each element of a matrix, are accomplished by proceeding the desired operator with a period (.):
>> [1 2 3; 4 5 6].^2
ans =
1 4 9
16 25 36
- Matlab functions can be combined in a single statement. For example, the mathematical operation represented by
can be implemented in Matlab as:
>> sum( log( abs( x(1:N) ) ) )
- The clear function can be used to partially or completely erase any previously defined variables in your workspace.
- The reshape function can be used to resize an existing vector or matrix.