Page 26 - Applied Numerical Methods Using MATLAB
P. 26
BASIC OPERATIONS OF MATLAB 15
1.1.7 Operations on Vectors and Matrices
We can define a new scalar/vector/matrix or redefine any existing ones in terms
of the existent ones or irrespective of them. In the MATLAB Command window,
let us defineA and B as
3
123
A = , B = −2
456
1
by typing
>>A = [1 2 3;4 5 6], B = [3;-2;1]
We can modify them or take a portion of them. For example:
>>A = [A;7 8 9]
A=1 2 3
4 5 6
7 8 9
>>B = [B [1 0 -1]’]
B=3 1
-2 0
1 -1
Here, the apostrophe (prime) operator (’) takes the complex conjugate transpose
and functions virtually as a transpose operator for real-valued matrices. If you
want to take just the transpose of a complex-valued matrix, you should put a
dot(.)before ’,thatis, ‘.’’.
When extending an existing matrix or defining another one based on it, the
compatibility of dimensions should be observed. For instance, if you try to annex
a4 × 1matrixinto the 3 × 1 matrix B, MATLAB will reject it squarely, giving
you an error message.
>>B = [B ones(4,1)]
???All matrices on a row in the bracketed expression must have
the same number of rows
We can modify or refer to a portion of a given matrix.
>>A(3,3) = 0
A = 1 2 3
4 5 6
7 8 0
>>A(2:3,1:2) %from 2 nd row to 3 rd row, from 1 st column to 2 nd column
ans=4 5
7 8
>>A(2,:) %2 nd row, all columns
ans = 4 5 6
The colon (:) is used for defining an arithmetic (equal difference) sequence
without the bracket [] as
>>t = 0:0.1:2