0 likes | 14 Views
Matrices are essential in Matlab for linear algebra computations. They are two-dimensional arrays consisting of rows and columns. Learn how to create, manipulate, and perform operations on matrices using square bracket notation and common commands like transpose, identity matrix, addition, subtraction, scalar multiplication, matrix multiplication, inverse, powers, determinant, ones, and zeros. Get familiar with matrix addressing, generation, and manipulation to excel in Matlab programming.
E N D
• An m * n matrix is a two-dimensional array of numbers consisting of m rows and n columns. • Special cases are a column vector (n = 1) and a row vector • (m = 1). • Matrices are fundamental to Matlab and even if you are not intending to use Matlab for linear algebra computations you need to become familiar with matrix generation and manipulation
Matrices A Matrix array is two-dimensional, having both multiple rows and multiple columns, similar to vector arrays: it begins with [, and end with ] spaces or commas are used to separate elements in a row semicolon or enter is used to separate rows. •Example: •>> f = [ 1 2 3; 4 5 6] f = 1 2 3 4 5 6 A is an m x n matrix. the main diagonal
Matrices can be built explicitly using the square bracket notation. • For example, a 3-by-3 matrix comprising the rst 9 primes can be set up with the command A = [2 3 5 7 11 13 17 19 23] • The end of a row can be specified by a semicolon instead of a carriage return, so a more compact command with the same effect is “Semi-colons separate Rows” • A = [2 3 5; 7 11 13; 17 19 23] • Within a row, elements can be separated by spaces or by commas \Commas separate Columns" .
Matrix Addressing -- matrixname(row, column) -- colon may be used in place of a row or column reference to select the entire row or column. Example: recall: f = >> f(2,3) 1 2 3 4 5 6 h = 2 4 6 1 3 5 ans = 6 >> h(:,1) ans = 2 1
Matrices (con’t…) more commands B = A’ eye(n) returns an n x n identity matrix eye(m,n) returns an m x n matrix with ones on the main diagonal and zeros elsewhere. C = A + B C = A – B B = A, where is a scalar. C = A*B B = inv(A), A must be a square matrix in this case. rank (A) returns the rank of the matrix A. B = A.^2 squares each element in the matrix C = A * A computes A*A, and A must be a square matrix. det (A), and A must be a square matrix. A, B, C are matrices, and m, n, are scalars. Transpose Identity Matrix Addition and subtraction Scalar Multiplication Matrix Multiplication Matrix Inverse Matrix Powers Determinant
eye eye Identity matrix I = eye returns the scalar, 1. I = eye(n) returns an n-by-n identity matrix with ones on the main diagonal and zeros elsewhere. I = eye(4) I = 4×4 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 I = eye(n,m) returns an n-by-m matrix with ones on the main diagonal and zeros elsewhere. I = eye(2,3) 1 0 0 0 1 0
eye eye Identity matrix • The values of the vector in eye([2,3]) defines the size of the vector. • For example, eye([2,3]) returns a 2-by-3 array with ones on the main diagonal and zeros elsewhere. sz = [3,1]; I = eye(sz) I = 3×1 1 0 0
ones ones Create array of all ones Syntax X = ones %returns the scalar 1 X = ones(n) X=ones([2*3]) % returns an n*n matrix of ones Returns 2*3 array of ones
zeros zeros Create array of all zeros Syntax X = zeros %returns the scalar 0 X = zeros(n) X = zeros(sz) % returns an n*n matrix of zeros % returns an array of zeros where the size vector, sz, defines size (x) For example A = [1 4; 2 5; 3 6]; sz = size(A); X = zeros(sz) X = 3×2 0 0 0 0 0 0