Download presentation
Presentation is loading. Please wait.
Published byAngela Claire Baldwin Modified over 9 years ago
1
Matlab Chapter 2: Array and Matrix Operations
2
What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters. In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters. Vector is one row OR one column Vector is one row OR one column –Does not have to have anything to do with geometry –Number of components is not restricted –Transpose of row is column, of column is row
3
Vectors in Matlab Various forms for entering row and column vectors: Various forms for entering row and column vectors: >> p=[1,2,3] p = 1 2 3 1 2 3 >> u=[1;2;3;4] u = 1 2 3 4 >> v=[3:2:9] v = 3 5 7 9 >> w=[4:7] w = 4 5 6 7
4
Transposing Vectors >> p p = 1 2 3 1 2 3 >> p' ans = 1 2 3 >> u u = 1 2 3 4 >> u' ans = 1 2 3 4 1 2 3 4
5
Augmenting Row Vectors >> p p = 1 2 3 1 2 3 >> v v = 3 5 7 9 3 5 7 9 >> z=[p v] z = 1 2 3 3 5 7 9 1 2 3 3 5 7 9
6
Stacking Column Vectors >> u u = 1 2 3 4 >> t t = 3 8 -4 >> q=[u;t] q = 1 2 3 4 3 8 -4
7
Functions to Generate Vectors linspace(a,b,c) produces c evenly spaced points between a and b linspace(a,b,c) produces c evenly spaced points between a and b >> linspace(2,4,5) ans = 2.0000 2.5000 3.0000 3.5000 4.0000 2.0000 2.5000 3.0000 3.5000 4.0000 logspace(a,b,n) computes n pts linearly between a and b, then uses them as exponents of 10 >> logspace(1,3,4) ans = 1.0e+003 * 0.0100 0.0464 0.2154 1.0000
8
Matrices A rectangular array of numbers or characters A rectangular array of numbers or characters –Rows numbered top to bottom –Columns numbered left to right –Size of array: n x m »number of rows is always stated first –element of array: x(k,j) »row index is always stated first—kth row, jth col –In general, indices can be negative or zero, but not in MATLAB –Transpose of real matrix interchanges rows and columns
9
Transpose & Concatenate Matrices >> A=[1,2,3;4,5,6] >> A=[1,2,3;4,5,6] A = 1 2 3 1 2 3 4 5 6 4 5 6 A’ is transpose >> A' ans = 1 4 1 4 2 5 2 5 3 6 3 6 >> B=[4,5;8,9] B = 4 5 8 9 [A,B] augments (concatenates rows) ans = 1 2 3 4 5 4 5 6 8 9 [A’;B] stacks (concatenates cols) ans = 1 4 2 5 3 6 4 5 8 9
10
Vector Addressing »v = [1,7,3,8,6,7,3] v(4) returns the 4 th element >> v(4)v(4) returns the 4 th element >> v(4) ans = 8 Note: parentheses, not square brackets! v(2:5) returns elements 2 through 5v(2:5) returns elements 2 through 5 >> v(2:5) ans = 7 3 8 6
11
Vector Addressing cont. »v = [1,7,3,8,6,7,3] v(:) returns the entire vector as a columnv(:) returns the entire vector as a column >> v(:) ans = 1 7 3 8 6 7 3
12
Matrix Addressing A = 3 6 8 3 6 8 1 5 2 1 5 2 A(2,1) returns the element in the 2 nd row, 1 st column »ans = 1 A(:,3) returns the 3 rd column »ans = 8 2 A(2,:) returns the 2 nd row »ans = 1 5 2 A(1:2,2:3) returns all elements in the 1 st and 2 nd rows that are also in 2 nd and 3 rd columns
13
Matrix Addressing A = 3 6 8 3 6 8 1 5 2 1 5 2 A(1:2,2:3) returns all elements in the 1 st and 2 nd rows that are also in 2 nd and 3 rd columns ans = 6 8 A(1:2,2:3) returns all elements in the 1 st and 2 nd rows that are also in 2 nd and 3 rd columns ans = 6 8 5 2 5 2 Extract smaller array: Extract smaller array: »B = A(1:2,[1 3]) B = 3 8 1 2
14
Arrays/Matrix Addressing Empty or null array Empty or null array »[ ] A = 3 6 8 3 6 8 1 5 2 1 5 2 –Remove a column: »A(:,3) = [ ] A = 3 6 1 5 –Automatic enlargement: »A(3,5) = 22 A= 3 6 0 0 0 1 5 0 0 0 0 0 0 0 22 1 5 0 0 0 0 0 0 0 22
15
Arrays/Matrix Addressing –Negative increment reverses order: p=[3,8,4,6]; p=[3,8,4,6]; »rev = p(end:-1:1) rev = 6 4 8 3 –A(n) returns the n th element of matrix A, going column by column A = 3 6 8 1 5 2 1 5 2 »A(4) ans = 5
16
Arrays/Matrix Addressing A(:) returns all elements as a column, going column by column A = 3 6 8 1 5 2 »A(:) ans = 3 1 6 5 8 2
17
Automatic growth of a vector If vector already defined as row or column, assigning new element beyond current bounds automatically grows to fit with intervening elements set to 0 If vector already defined as row or column, assigning new element beyond current bounds automatically grows to fit with intervening elements set to 0 »x = [1;2]% x starts as 2-element column vector »x(5) = 24% x now has 5 elements. x(3) & x(4) = 0 x = 1 2 0 0 24 24
18
Automatic growth of a vector: –If variable is not yet vector, assigning element beyond 1 defaults to creating a row vector >>clear xx >>clear xx »xx(4) = 12% xx starts as a 4-element row vector xx = 0 0 0 12 0 0 0 12
19
Warning! Use clear to Avoid Errors Use clear to Avoid Errors –Be careful: suppose you simply want A to have u and v as columns: »A(:,1) = u; »A(:,2) = v; –A however, still has its previous columns! –Use clear to avoid this: »clear A
20
Vector and Matrix Functions CommandDescription cat(n,A,B,C, …) concatenates A,B,C,… along dimension n find(x) indices of nonzero elements of array x length(A) vector # of elements, matrix max. of m or n max(A) matrix largest elements of each column [x,k] = max(A) stores max.’s in x, indices in k min(A) same as max but minimum values [x,k] = min(A) same as [x,k] = max(A) but minimum values size(A) returns dimensions of array A sort(A) sorts each column in ascending order sum(A) returns vector of column sums
21
Array Operations Same operation performed on each corresponding element of array A = 3 7 2 3 7 2 6 -1 5 6 -1 5 >> A+3 ans = 6 10 5 6 10 5 9 2 8 9 2 8 >> 2*A ans = 6 14 4 6 14 4 12 -2 10 12 -2 10 >> A.^2 ans = 9 49 4 9 49 4 36 1 25 36 1 25 B = 4 6 1 3 9 2 >> A+B ans = 7 13 3 9 8 7 >> A.*B ans = 12 42 2 18 -9 10 A./B ans = 0.7500 1.1667 2.0000 2.0000 -0.1111 2.5000
22
Math Functions Automatically Apply to Each Element A = 3 7 2 6 -1 5 >> sin(A) ans = 0.1411 0.6570 0.9093 -0.2794 -0.8415 -0.9589 >> sqrt(A) ans = 1.7321 2.6458 1.4142 2.4495 0 + 1.0000i 2.2361
23
Vector Operations u = 3 2 5 3 2 5 v = -4 5 2 >> dot(u,v) ans = 8 >> cross(u,v) ans = -21 -26 23 >> norm(u) ans = 6.1644 >> %this is the Euclidean length or magnitude
24
Matrix Product C= A*B is defined if and only if the number of columns in A equals the number of rows in B. Then C will have the same number of rows as A and the same number of columns as B (3x2)*(2x4) produces a 3x4 (3x2)*(3x2) not defined (3x2)*(2x3) produces a 3x3 (2x3)*(3x2) produces a 2x2
25
Matrix Product C=A*B The (i,j) entry in C is the sum of the products of entries from the i-th row of A and the j-th column of B A = 4 5 -1 3 2 0 B = 3 2 1 5 1 -1 0 3 2 5 1 4 >> A*B ans = 15 -2 3 31 11 4 3 21
26
Matrix Power A^n means multiply A by itself n times. A^n means multiply A by itself n times. –Can only be done if A is square A.^n means raise each element of A to the n-th power A.^n means raise each element of A to the n-th power –Can be done with any array A
27
Matrix Division If A and B are matrices, A/B is NOT a defined operation in linear algebra! If A and B are matrices, A/B is NOT a defined operation in linear algebra! Matlab uses both right (/) and left (\) operator symbols for special matrix operations we are not covering in this course! Matlab uses both right (/) and left (\) operator symbols for special matrix operations we are not covering in this course! 1./A is an array operation creating a new array with each entry being the reciprocal of the entry in A 1./A is an array operation creating a new array with each entry being the reciprocal of the entry in A A./B is an array operation creating a new array with each entry being the corresponding entry in A divided by the corresponding entry in B A./B is an array operation creating a new array with each entry being the corresponding entry in A divided by the corresponding entry in B
28
Special Matrices eye(n)—nxn “identity” matrix eye(n)—nxn “identity” matrix –eye(3) 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1 ones(m,n)– mxn matrix with all entries = 1 ones(m,n)– mxn matrix with all entries = 1 zeros(m,n)—mxn matrix with all entries =0 zeros(m,n)—mxn matrix with all entries =0
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.