Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars, vectors and matrices generally as arrays. The following classes of Array are now availble in MATLAB: ArrayNumericCharacterLogicalCellStructure Function handle Java
Numeric Arrays So far we have used only numeric arrays, which are arrays containing only numeric values. A character array is an array containing strings. The elements of Logical arrays are "true" or "false," which, although represented by the symbols 1 and 0, are not numeric quantities.
One Dimensional Array/ Vectors Row Vector Column Vector A vector is a special type of matrix, having only one row, or one column. Vectors are also called lists or arrays in other programming languages. Two Dimensional Array An array can have multiple rows or column or both, such a two dimensional array is called a matrix. Numeric Arrays
The official MATLAB documentation refers to all variables as arrays, whether they are single-valued (scalars) or multi-valued (vectors or matrices). In other words, a scalar is a 1x1 array, i.e. an array with a single row and a single column which, of course, is an array of one item. Rectangular Matrix: Scalar:1-by-1 array Vector:m-by-1 array 1-by-n array Matrix:m-by-n array Numeric Arrays
? X=[2 7 4] X=[2; 7; 4] or X=[2 7 4]’ X=[2 7 4;3 8 9] Y=[X X] Row Vector Column Vector Matrix or a 2D array Matrix of matrices Numeric Arrays
Vector & Matrix in MATLAB Rows (m) Columns (n) A = A (2,4) A (17) >>A=[ ]; >>A=[ ; ; ; ; ]; A (5) A(5,1) A (17)
More on Vectors x = start:endCreates row vector x starting with start, counting by 1, ending at end x = initial value : increment : final value Creates row vector x starting with start, counting by increment, ending at or before end x = linspace(start,end,number)Creates linearly spaced row vector x starting with start, ending at end, having number elements x = logspace(start,end,number)Creates logarithmically spaced row vector x starting with start, ending with end, having number elements length(x)Returns the length of vector x y = x’Transpose of vector x dot(x,y),cross(x,y)Returns the scalar dot and vector cross product of the vector x and y
Numeric Arrays Exercise Find Even Numbers between 1 to 10 >> A=[2:2:10] Fine Odd Numbers Between 1 to 10 >> B=[1:2:9] Find Even Numbers between 1 to 100 >> C=[2:2:100] Fine Odd Numbers Between 1 to 100 >> D=[1:2:99]
More on Matrices zeros(n) Returns a n ⅹ n matrix of zeros zeros(m,n) Returns a m ⅹ n matrix of zeros rand(m,n) Returns a m ⅹ n matrix of random numbers eye(m,n) Returns a m ⅹ n Identity matrix ones(n) Returns a n ⅹ n matrix of ones ones(m,n) Returns a m ⅹ n matrix of ones size(A) For a m ⅹ n matrix A, returns the row vector [m,n] containing the number of rows and columns in matrix length(A)Returns the larger of the number of rows or columns in A
Any MATLAB expression can be entered as a matrix element Entering Numeric Arrays »a=[1 2;3 4] a = »b=[-2.8, sqrt(-7), (3+5+6)*3/4] b = i »b(2,5) = 23 b = i »a=[1 2;3 4] a = »b=[-2.8, sqrt(-7), (3+5+6)*3/4] b = i »b(2,5) = 23 b = i Row separator: semicolon (;) Column separator: space / comma (,) Use square brackets [ ] Matrices must be rectangular/same height. (Set undefined elements to zero)
Any MATLAB expression can be entered as a matrix element Entering Numeric Arrays Row separator: semicolon (;) Column separator: space / comma (,) Matrices must be rectangular/same height. (Set undefined elements to zero) MATLAB does not allow this !
Entering Numeric Arrays - cont. »w=[1 2;3 4] + 5 w = »x = 1:5 x = »y = 2:-0.5:0 y = »z = rand(2,4) z = »w=[1 2;3 4] + 5 w = »x = 1:5 x = »y = 2:-0.5:0 y = »z = rand(2,4) z = Scalar expansion Creating sequences colon operator (:) Utility functions for creating matrices.
Array Addressing Array indices are the row and column numbers of an element in an array and are used to keep track of the array 's elements. For example, the notation v (5) refers to the fifth element in the vector v, and A (2, 3) refers to the element in row 2, column 3 in the matrix A. The row number is always listed first! This notation enables you to correct entries in an array without retyping the entire array, For example, to change the element in row I, column 3 of a matrix D to 6, you can type D (1, 3) = 6. Numeric Arrays
Array Subscripting / Indexing Array indices are the row and column numbers of an element in an array and are used to keep track of the array 's elements A = A(3,1) A(3) A(1:5,5) A(:,5) A(21:25) A(4:5,2:3) A([9 14;10 15]) Use () parentheses to specify index colon operator (:) specifies range / ALL [ ] to create matrix of index subscripts ‘end’ specifies maximum index value A(1:end,end) A(:,end) A(21:end)’
Element-by-Element Operations >>A=[1 2;4 5]; >>B=A*A % prints % Proper matrix multiplication >>B=A.*A % prints % Element by element multiplication
Element-by-Element Operations
Operations on Matrices TransposeB=A’ Identity Matrix 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 Addition and SubtractionC =A +B C =A - B Scalar MultiplicationB = α A, where α is a scalar Matrix MultiplicationC = A * B Matrix InverseB = inv(A), A must be a square matrix in this case Matrix powersB = A * A, A must be a square matrix Determinantdet(A), A must be a square matrix
Operations on Matrices
Operating on Matrices
Operating on Matrices - cont. Matrices must have the same dimensions Dimensions of resulting matrix = dimensions of multiplied matrices Resulting elements = product of corresponding elements from the original matrices Same rules apply for other array operations Array Multiplication »a = [ ; ]; »b = [1:4; 1:4]; »c = a.*b c = »a = [ ; ]; »b = [1:4; 1:4]; »c = a.*b c = c(2,4) = a(2,4)*b(2,4)
String Arrays Created using single quote delimiter (') Each character is a separate matrix element (16 bits of memory per character) Indexing same as for numeric arrays »str = 'Hi there,' str = Hi there, »str2 = 'Isn't MATLAB great?' str2 = Isn't MATLAB great? »str = 'Hi there,' str = Hi there, »str2 = 'Isn't MATLAB great?' str2 = Isn't MATLAB great? 1x9 vector str = Hithere,
Elemantary Mathematical (Trigonometric) Functions Trigonometric functions Remarks sin(x) cos(x) tan(x) asin(x) acos(x) atan(x) atan2(y,x) sinh(x) cosh(x) tanh(x) asinh(x) acosh(x) atanh(x) - pi/2 ≤ atan(x) ≥ pi/2, Same as atan(y/x) but –pi ≥ atan(y,x) ≥ pi Mathematical Functions of MATLAB-1
Other elemantary functions Remarks abs(x) angle(x) sqrt(x) real(x) imag(x) conj(x) round(x) fix(x) floor(x) ceil(x) sign(x) exp(x) log(x) log10(x) factor(x) Absolute value of x Phase angle of complex value: If x = real, angle = 0. If x = √-1, angle = pi/2 Square root of x Real part of complex value x Imaginary part of complex value x Complex conjugate x Round to do nearest integer Round a real value toward zero Round x toward - ∞ Round x toward + ∞ +1 if x > 0; -1 if x < 0 Exponential base e Log base e Log base 10 1 if x is a prime number, 0 if not Mathematical Functions of MATLAB-2
Numeric Arrays Assignment No 3 Assume Scalar Values of h, b, r, h1, h2 Find A tringle, A rectangle, A circle and A trapezidal b h b h r b h1 h2