Arrays & Matices
Arrays & Matices
MATLAB
by
Mohamed Hussein
compliments to
Prof. Michael Negnevitsky
Univerity of Tasmania
Lecture 2
Introduction to Matlab
Array
Mathematics
Matrices and Matrix Manipulation
Matrix Operations
Scalar-Array Mathematics
>> a = [1 2 3 4];
>> a - 2
ans =
-1 0 1 2
>> 2*a + 1
ans =
3 5 7 9
Array-Array Mathematics
Array Orientation
Row vector
>> a = [1 2 3 4]
a=
1 2 3 4
Column vector
>> b = [1; 2; 3; 4]
b=
1
2
3
4
3
6
>> D = [A;B]
D=
1
2
4
5
4
5
1
2
3
6
6
3
or
>> C = [A, B]
4
1
5
2
6
3
>> A = [1 2 3;4 5 6]
A=
1
2
3
4
5
6
>> [x,y]=find(A>4)
x=
2
2
y=
2
3
How about
6
4
1.5000
2.0000
4.0000
3.0000
12
18
-6
6
How about
0.2500
2.2000
0.3333
2.0000
A .\ B?
Matrix Operations
Product (multiplication) of two matrices AB is obtained using *
operation (not .*). The number of columns in A must equal to the
number of rows in B. The result of the product is a matrix with the same
number of rows as A and the same number of columns as B i.e if matrix
A has a size of m n and B has a size of n p, the product will be a
matrix of size m p
>> A = [1; 2; 3]; B=[4, 5, 6];
>> A*B
ans =
4 5 6
8 10 12
12 15 18
>> A*B
ans =
32
Matrix Operations
Special Matrices
>> a = zeros(2)
a=
0
0
0
0
a 2-by-2 matrix of zeros.
>> a = ones(2,3)
a=
1
1
1
1
1
1
a 2-by-3 matrix of ones.