Arrays and Matrices
Arrays and Matrices
One of the strengths of MATLAB is the capability to handle collections of numbers, called arrays, as if they were a single variable. Single numbers are called scalars, whereas, arrays are collections of scalars arranged in a specific order. An example of an array variable is one that contains the numbers 0, 1, 3, and 6, in the order. We can use square brackets to define the variable x to contain this collection by typing >>x = [ 0, 1, 3, 6].
The elements of the array may be separated by commas or spaces. A one-column array or one-row array is called vector. Thus, v1 = [5 7 2] is a row-vector 5 and 2 = 7 is a column vector. 2
In general, array can have more than one column and more than one row. Furthermore, a two-dimensional array is called a matrix. An m x n matrix with m row and n columns. Example: 2 3 = 6 0 is a 3 x 2 matrix. 1 2
CREATING ARRAYS
A. ROW VECTOR
Type the elements inside a pair of square brackets, separating the elements inside with a space or a comma. Example: >>A = [10 3 4 6] or >>A = [10, 3, 4, 6]
B. COLUMN VECTOR
Separate the elements by semicolons or create a row vector first and then use the transpose notation ( ) which converts a row vector into a column vector, or vice versa. Example: >>B=[5; 3; 6; 0; 2] or >>B=[5, 3, 6, 0, 2]
Another way is to type a left bracket and the first element, press ENTER, type the second element, press ENTER, and so on until you type the last element followed by the right bracket. Example: >>B=[5 3 6 0 2]
C. MATRICES
Type the matrix row by row, separating the elements in a given row with spaces or commas and separating the rows with semicolons. Example: 7 4 3 To create = , type 1 2 4 >>A=[7, 4, 3; 1, 2, 4]
APPENDING ARRAYS
Array can be created by appending one array to anther. Illustration: 1. Let array1 = [4, 6, 2] and array2 = [2, 5]. Type >>array3 = [array1, array2] 4 2 6 2 4 2. Let = and = 7 0 9 5 1 Typing >>C = [A , B]
Example:
>>x = [2: 2: 8] >>y = [1: 3: 11] >>z = [20: 2: 10] If q is omitted, it is presumed to be 1. Example: >>w = [3: 2]
2. Using linspace Linspace command creates a linearly spaced row vector by specifying the number of elements rather than the increment. The syntax >>x = [a, b, n] means creating a vector x where a and b are the lower and upper limits, respectively and n is the number of elements. Example: >>linspace (5, 8, 31). It contain 31 elements. If n is omitted, the default value is 100.
3. Using logspace It creates an array of logarithmically spaced elements. The syntax is >>logspace (a, b, n) where n is the number of elements between 10a and 10b. Example: >>x = logspace (1, 1, 6) If n is omitted, the number of elements defaults to 50.
size(A)
returns a row vector [m, n] containing the sizes of the m x n array A length(A) computes either the number of elements of A if A is a vector or the largest value of m or n if A is an m x n matrix sum(A) sums the elements in each column of the array A and returns a row vector containing the sums sort(A) sorts each column of the array A in ascending order and returns an array the same size as A
max(A)
min(A)
returns the algebraically largest element in A if A is a vector. Returns a row vector containing the largest elements in each column if A is a matrix same as max(A) but returns minimum values
diag(A)
Practice Set: 1. Let A = [1, 10, 6, 4; 7, 2, 5, 11; 12, 0, 0, 8; 1, 3, 5, 6]. Find: sum (A), max(A), min(A), length(A), size(A), sort(A), diag(A) 2. Let B = magic(6). Verify that the sum of the rows, columns and diagonals are equal. 3. Create an identity matrix with a size as matrix A in problem 1.
ARRAY ADDRESSING/INDEXING
Array indices are the row and column numbers of an element in an array and are used to keep track of the arrays elements. Illustration:
Address v(2) Description refers to 2nd element in the vector v
A(3,4)
v(:)
represents the 1st through 3rd elements of v A(:, 2) denotes all the elements in the 2nd column of A A(:, 1:4) denotes all the elements in the 1st through 4th column of A A(3, :) denotes all the elements in the 3rd row of A A(2:4, :) denotes all the elements in the 2nd through 4th row of A A(2:3,1:3) denotes all the elements in the 2nd through 3rd row that are also in the 1st v(1:3)
Practice Set: 1. Let v = [4, 2, 6, 0, 8] and A = [1, 7, 4, 0; 1, 5, 3, 2; 3, 1, 8, 5]. Determine the output of the following: a. v(5) b. v(2:4) c. A(2,4) d. A(3,2) e. A(:, 3) f. A(:, 2:4) g. A(2,:) h. A(2:3, :)
3 2 1 3. Given: = = 5 4 2 Find: 4, 2,
1 2
4 6
4. Given:
2 3 1 1 5 = 0 4 2 = 2 2 1 5 1 3 1 0 1 0 = 4 4 2 2 6 5
MATRIX OPERATIONS
Applies the same rule in ordinary matrices operations in Mathematics. Note: 1. + and apply the same rule as in elementby-element operations. 2. A/B = A*B-1 , where B-1 is the inverse of B. 3. A^2 = A*A 4. AB is not defined .
Example: Let M = [1, 3, 6; 2, 4, 0; 5, 8, 9], N = [4, 2; 6, 3; 1, 5] and O = [2, 2, 1; 3, 4, 0; 1, 3, 7]. Find: a. MN b. M2 c. Show that MM-1=1 d. Show that M/O = MO-1
Use the command inv(A) to evaluate the inverse of matrix A.
Determinants
Use the command det(A) to evaluate the determinant A. Examples: Evaluate the following determinants:
3 a. 9 5 12 0 2 3 0 c. 6 4 3 2 5 5 0 5 4 4 7 0
2 3 1 b. 1 1 2 3 4 1
11 1 + 12 2 + + 1 = 1 21 1 + 22 2 + + 2 = 2 1 1 + 2 2 + + =
An Example
Consider the system of 3 equations and 3 unknowns: 21 + 32 3 = 1 1 + 22 + 33 = 9 2 + 23 = 5
2 = 1 0 3 1 1 1 2 3 , = 9 , so = 1 = 1 1 2 5 2
An Example
Using MatLab: >> A=[2 3 -1;-1 2 3;0 1 2] A= 2 3 -1 -1 2 3 0 1 2 >> b=[-1 9 5]' b= -1 9 5
An Example
Using MatLab: >> x=inv(A)*b x= -1 1 2
Example
0 1 If = 2 3
1 = 2 + 3 + 2 = 0 2 3
Example
and the two eigenvalues are 1 = 1, 2 = 2
All that's left is to find the two eigenvectors. Let's find the eigenvector, v1, associated with the eigenvector, 1, first. 1 = 1 1 = 0 1 1 = 0 2 3 1 1,1 1 1 =0 1,2 2 2
Example
so clearly
1,1 + 1,2 = 0, and 2 1,1 + 2 1,2 = 0, so 1,1 = 1,2 and the first eigenvector is any 2 element column vector in which the two elements have equal magnitude and opposite sign. +1 1 = 1 1 where k1 is an arbitrary constant. Note that we didn't have to use +1 and -1, we could have used any two quantities of equal magnitude and opposite sign.
Example
Going through the same procedure for the second eigenvalue: = 2 2,1 2 1 =0 2,2 2 1 2 2,1 + 2,2 = 0, and 2 2,1 + 2,2 = 0, so 2 2,1 = 2,2 and +1 2 = 2 2
Using Matlab
A=[0 1;-2 -3] A= 0 1 -2 -3 [v,d]=eig(A) v= 0.7071 -0.4472 -0.7071 0.8944 d= -1 0 0 -2
Using Matlab
The eigenvalues are the diagonal of the "d" matrix; 1=-1, 2=-2. The eigenvectors are the columns of the "v" matrix. Note that MatLab chose different values for v1,1, etc..., but that the ratio of v1,1 to v1,2 and the ratio of v2,1 to v2,2 are the same as our solution. (MatLab chooses the values such that the sum of the squares of the elements of the eigenvector equals unity).