Matlab Session 3
Matlab Session 3
Matrices
Matlab has a number of built-in routines to help create matrices. You can create a matrix of zeros of any size.
>> A=zeros(5) A= 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
>> B=zeros(3,5) B= 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
You easily can create a matrix of zeros that is the same size as a given matrix.
>> C=[1 2 3;4 5 6;7 8 9] C=
1 4 7
2 5 8
3 6
9
>>D=zeros(size(C)) D= 0 0 0 0 0 0 0 0 0
1 1 1 1
1 1 1 1
Page | 1 of (3)
1 1
1 1
1 1
1 1
1 1
1 1
>> B=ones(2,10) B= 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>>D=ones(size(C)) D= 1 1 1 1 1 1 1 1 1
The identity matrix has ones on its main diagonal and zeros everywhere else.3
>> I=eye(5)
Other types of diagonal matrices are possible with Matlabs diag command. >> E=diag([1,2,3,4,5]) E=
The transpose operator (single apostrophe) plays the same role with matrices as it does with vectors. Rows are changed to columns and columns are changed to rows. >> J=[1 2 3;4 5 6;7 8 9] >> J'
Two online calculators and solvers for systems of 2 by 2 and 3 by 3 linear equations. 2 by 2 systems of linear equations are of the form ax+by=c dx+ey=f A*Z=B Page | 2 of (3)
Where Z = A-1*B
Ex: Solve The system of equations x1 + 2x2 3x3 = 4 2x1 3x3 = 2 x2 + x3 = 0 solution
>>A = [1 2 -3;2 0 -3;0 1 1] A= 3- 2 3- 0 1 1 1 2 0
Page | 3 of (3)