150 likes | 302 Views
Chapter 2. Creating Arrays Legend: MATLAB command or syntax : Blue MATLAB command OUTPUT : LIGHT BLUE. Vector – a 1D row or column Several ways of creating a vector: 1. Specifying EACH element. Row Vector. Column Vector. Elements separated by semi-colon (;) . e.g.
E N D
Chapter 2 Creating Arrays Legend: MATLAB command or syntax : Blue MATLAB command OUTPUT : LIGHT BLUE
Vector – a 1D row or columnSeveral ways of creating a vector:1. Specifying EACH element Row Vector Column Vector Elements separated by semi-colon (;) . e.g. q = [1; 2; 3; 4; 5] q = 1.00 2.00 3.00 4.00 5.00 • Elements separated by space or comma. • e.g. p = [1 2 3 4 5] OR p = [1, 2, 3, 4, 5] p = 1.00 2.00 3.00 4.00 5.00
Several ways of creating a vector:2. Specify 1st term, last term, and spacing(constant) between each element variable_name = [m : q : n] OR variable_name = m : q : n *spacing could be positive or negative.*If spacing is omitted, the default is 1. e.g. a = [1 : 2 : 11] a = 1 3 5 7 9 11 b = [12 : -3 : 0] b = 12 9 6 3 0 c = [1 : 5] ?? c = 1 2 3 4 5 last term 1st term spacing
Several ways of creating a vector:2. Specify 1st term, last term, and number of terms • variable_name = linspace(f, l, n) where f: first term; l : last term ; n : # of terms * If n is omitted, default is 100. • e.g. v = linspace(0, 10, 5) v = 0 2.50 5.00 7.50 10.00 • e.g. w = linspace(1, 50, 1000);
Transpose of a Vector • Row and column vectors are transpose of each other. • To convert a row vector a to a column vector b, type the transpose operator, a single quote (‘) after the vector name. • e.g. >> a = linspace(2,10,5) a = 2 4 6 8 10 >> b = a' b = 2 4 6 8 10
Addressing elements of a vector • Addressing a range of elements. For example, addressingelementsbetween and including positions 2 and 5 >> a = v(2:5) a = 8 6 4 2 • Re-assignvalue of 0 to 5th element. >> v(5) = 0 v = 10 8 6 4 0 0 >> v(1) * v(5) + v(2) ans = 8 • Position of the 1st element in a vector is 1. • So, for a vector v, the element is addressed as v(k). • e.g. >> v = [10 : -2 : 0] v = 10 8 6 4 2 0 • Addressingspecificelements >> v(1) * v(5) + v(2) ans = 28 • Addressing ALL elements of vector >> a = v(:) a = 10 8 6 4 2 0
Manipulating Vectors (Adding and Deleting Elements) • Add elements to vector by assigning new elements. • e.g. >> v = [1 2 3 4 ] • v = 1 2 3 4 >> v(6) = 6 v = 1 2 3 4 0 6 • Delete elements from vectors : by assigning element(s) to [] • e.g. >> v(5) = [] • v = 1 2 3 4 6 • >> v(2:3) = [] v = 1 4 6
Facts about Matrices • A 2D array of rows and columns. • An mXn matrix has m rows and n columns. • Square Matrix : # of rows = # of columns = n(say) Then square matrix would be nXn • Identity Matrix : Diagonal elements are ones; non-diagonal elements are zeros • Zero matrix : All elements are zeros.
Facts about Matrices • Examples of matrices • A 3X4 matrix (3 rows and 4 columns) 1 2 3 4 5 6 7 8 9 10 11 12 • A 3X3 identity matrix 1 0 0 0 1 0 0 0 1 • A 2X3 zero matrix 0 0 0 0 0 0
Matrices in MATLAB • Creating a matrix Variable_name = [r1e1 r1e2 ..…r1en; r2e1 r2e2… r2en;……. rne1 rne2 …rnen] where r1e1 : row 1 element 1 and so on • e.g. a = [1 2 3 ; 4 5 6 ; 7 8 9] a = 1 2 3 4 5 6 7 8 9 *elements of a matrix(or vector) can be numbers, math expressions, predefined variables and/or functions.
Matrices in MATLAB • Zero matrix : zeros(m,n) • Ones matrix : ones(m,n) m : # of rows; n : # of columns • Identity matrix : eye(n) N : # of rows and columns e.g. >> zeros(2,3) ans = 0 0 0 0 0 0 >> eye(3) ans = 1 0 0 0 1 0 0 0 1 • Transpose of a matrix : rows and columns are interchanged. e.g. >> a = [1 2 3;4 5 6;7 8 9] a = 1 2 3 4 5 6 7 8 9 >> b = a' b = 1 4 7 2 5 8 3 6 9
Addressing Matrices • By specifying the row and column of the element, we specify its position. • Position of 1st element of a matrix A is A(1,1). • A(:,n) Refers to elements in ALL ROWS of column n of matrix A. • A(m,:) Elements in ALL COLUMNS and row n. • A(:,m:n) Elements in ALL ROWS of columns m through n • A(m:n,:) Elements in ALL COLUMNS and rows m throughn. • A(m:n,p:q) Elements in rows m through n and columns p through q. Addressing Matrices
Manipulating Matrices (Adding and Deleting Elements) Adding elements to matrix: >> V= [1 2 3; 4 5 6] V = 1 2 3 4 5 6 >> V(5,:) = ones(1,3) V = 1 2 3 4 5 6 0 0 0 0 0 0 1 1 1 Deleting elements from matrix >> V(3:4, :) = [] V = 1 2 3 4 5 6 1 1 1 V(2,2) =[] ?? ??? Subscripted assignment dimension mismatch.
Other Built-In Functions Vectors • length(v) : returns number of elements in vector v • diag(v) : creates a square matrix (from the vector v), with elements of v in the diagonal. e.g. >> v = [1 2 3 ]; >> d = diag(v) d = 1 0 0 0 2 0 0 0 3 >> l = length(v) l = 3 Matrices • Size(A) : returns row vector [m,n] , where m and n are size m X n of the matrix. • diag (A) : Creates a vector from the diagonal elements of matrix A. • reshape (A,m,n) : creates an m X n matrix from A. elements are taken column after column. A must have m times n elements. e.g. >> A = [1 2 3 ; 4 5 6]; >> s = size(A) s = 2 3 d = (diag(A))' d = 1 5 >> B = reshape(A, 2,3) B = 1 2 3 4 5 6
Strings • String is an array of characters. • It can include letters, digits, other symbols, and spaces. • It’s created by typing characters within single quotes. e.g. ‘MATLAB’, ‘34&g’ , ‘h!#ji464’ . e. g.>> S = 'It is MATLAB time!!' S = It is MATLAB time!! >>size(S) ans= 1 19 • Are n = 123 and s = ‘123’ same?? NO >> size(n) ans= 1 1 >> size(s) ans= 1 3 • To create a matrix of strings, each row should have the same number of elements. This can be done using a built-in function called char. • Char creates an array with each row having same number of elements. It makes the length of each row same as the longest row by adding spaces. e.g. >> Emp = char('Employee Name', 'Employee Number', 'Department') Emp= Employee Name Employee Number Department