Upload
christiana-jordan
View
214
Download
0
Embed Size (px)
Citation preview
Philosophiae Naturalis Principia Matlabematica
Damian Gordon
Getting to Matlab
Magic Matrix
MAGIC Magic square.
MAGIC(N) is an N-by-N matrix constructed from the integers
1 through N^2 with equal row, column, and diagonal sums.
Produces valid magic squares for N = 1,3,4,5,...
Identity Function
>> eye (4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Upper Triangle Matrix
» a = ones(5)
a =
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
» triu(a)
ans =
1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 0 1
Lower Triangle Matrix
» a = ones(5)
a =
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
» tril(a)
ans =
1 0 0 0 0 1 1 0 0 0 1 1 1 0 0 1 1 1 1 0 1 1 1 1 1
Hilbert Matrix» hilb(4)
ans =
1.0000 0.5000 0.3333 0.2500
0.5000 0.3333 0.2500 0.2000
0.3333 0.2500 0.2000 0.1667
0.2500 0.2000 0.1667 0.1429
Inverse Hilbert Matrix» invhilb(4)
ans =
16 -120 240 -140
-120 1200 -2700 1680
240 -2700 6480 -4200
-140 1680 -4200 2800
Toeplitz matrix.
TOEPLITZ
TOEPLITZ(C,R) is a non-symmetric Toeplitz matrix having C as its
first column and R as its first row.
TOEPLITZ(R) is a symmetric (or Hermitian) Toeplitz matrix.
-> See also HANKEL
Summary of Functions
• magic• eye(4)• triu(4)• tril(4)• hilb(4)• invhilb(4) • toeplitz(4)
- magic matrix
- identity matrix
- upper triangle
- lower triangle
- hilbert matrix
- Inverse Hilbert matrix
- non-symmetric Toeplitz matrix
Dot Operator
• A = magic(4); b=ones(4);
• A * B
• A.*B
• the dot operator performs element-by-element operations, for “*”, “\” and “/”
Concatenation
• To create a large matrix from a group of smaller ones
• try– A = magic(3)– B = [ A, zeros(3,2) ; zeros(2,3), eye(2)]– C = [A A+32 ; A+48 A+16]– Try some of your own !!
Subscripts
• Row i and Column j of matrix A is denoted by A(i,j)
• A = Magic(4)
• try– A(1,4) + A(2,4) + A(3,4) + A(4,4)
• try– A(4,5)
The Colon Operator (1)
• This is one MatLab’s most important operators
• 1:10 means the vector– 1 2 3 4 5 6 7 8 9 10
• 100:-7:50– 100 93 86 79 72 65 58 51
• 0:pi/4:pi– 0 0.7854 1.5708 2.3562 3.1416
The Colon Operator (2)
• The first K elements in the jth column is– A(1:K, j)
• Sum(A(1:4, 4)) is the sum of the 4th column
or
• Sum(A(:, 4)) means the same