MATLAB Lecture hoolofMathematicalSciencesXiamenUniversityhttp://gdjpkc.xmu.edu.cn MATLAB Lecture 1 Matrices and arrays 矩阵和数组 IATLAB→ Getting Started→ Matrices and Arrays MATLAB→ Getting Started→ Expressions MATLAB Mathematics Matrices and Linear Algebra Matrices in MAtLaB ● Vocabulary addition加法 substration减法 multiplication乘法 division除法 左除 power乘幂 complex conjugate transpose复共轭转置 explicit显的 lst序列,列表 element元素 enter/ input输入 matrIx( pl.matrices)矩阵 array数组 vector向量 variable变量 assignment statement赋值语句 expression表达式 data数据 row column 列 dimension维数 Using MATLAB as a Scatchpa Operators(运算符) Expressions use familiar arithmetic operators and precedence rules Addition Subtraction Multiplication Division Left division (described in"Matrices and Linear Algebra in the Matlab document ation) Power Complex conjugate transpose () Specify evaluation order >>pi*0.1^2 0.0314 > area pi*0.12 the area of a circle with radius 0.1 area
MATLAB Lecture 1 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec11 MATLAB Lecture 1 _ Matrices and Arrays 矩阵和数组 MATLAB → Getting Started → Matrices and Arrays MATLAB → Getting Started → Expressions MATLAB → Mathematics → Matrices and Linear Algebra → Matrices in MATLAB l Vocabulary: addition 加法 substration 减法 multiplication 乘法 division 除法 left division 左除 power 乘幂 complex conjugate transpose 复共轭转置 explicit 显的 list 序列, 列表 element 元素 enter / input 输入 matrix (pl. matrices) 矩阵 array 数组 vector 向量 variable 变量 assignment statement 赋值语句 expression 表达式 data 数据 row 行 column 列 dimension 维数 l Using MATLAB as a Scatchpad Operators (运算符) Expressions use familiar arithmetic operators and precedence rules. >> pi*0.1^2 ans = 0.0314 >> area = pi*0.1^2 % the area of a circle with radius 0.1 area =
MATLAB Lecture hoolofMathematicalSciencesXiamenUniversityhttp://gdjpkc.xmu.edu.cn 0.0314 > volume area*o. 5% the volume of a cylinder with radius 0.1 and vo⊥ume= 0.0157 >>(12+2*(7-4))/3^2 ans >>8=1-1/2+1/3-1/4+1/5-1/6 +1/7-1/8 0.6345 >>z1=3+4⊥ z1= 3.0000+4.00001 >z2=1+2*⊥ >>z3=2+exp(i*pi/6) >z=z1*z2/z3 MATLAB - Getting Started Matrices and Arrays - Expressions -Operators ● Enter matrices in matlaB(矩阵) P Enter an explicit list of elements Y Separate the elements of a row with blanks or commas 2 Use a semicolon :i to indicate the end of each row Y Surround the entire list of elements with square brackets,[ 1 Load matrices from external data files Generate matrices using built-in functions P Create matrices with your own functions in M-files Array: a collection of data values organized into rows and columns Vector: an array with only one dimension Matrix: an array with two or more dimensions Size: specified by the number of rows and the number of columns in the arra ith the number of rows mentioned first >>[1234 567891: create an arrays with 9 elements >>[1,2,3;4,5,6;7,8,9]% a3×3 matrIx 6 >>[123 456 789
MATLAB Lecture 1 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec12 0.0314 >> volume = area*0.5 % the volume of a cylinder with radius 0.1 and … length 0.5 volume = 0.0157 >> (12+2*(74))/3^2 ans = 2 >> S = 1 – 1/2 + 1/3 –1/4 + 1/5 – 1/6 ... + 1/7 – 1/8 S = 0.6345 >> z1 = 3 + 4i z1 = 3.0000 + 4.0000i >> z2 = 1 + 2 * i >> z3 = 2*exp(i*pi/6) >> z = z1*z2/z3 MATLAB → Getting Started → Matrices and Arrays → Expressions → Operators l Enter matrices in MATLAB (矩阵) ÿ Enter an explicit list of elements. You only have to follow a few basic conventions: ² Separate the elements of a row with blanks or commas. ² Use a semicolon ‘;‘ to indicate the end of each row. ² Surround the entire list of elements with square brackets, [ ]. ÿ Load matrices from external data files. ÿ Generate matrices using builtin functions. ÿ Create matrices with your own functions in Mfiles. Array: a collection of data values organized into rows and columns Vector: an array with only one dimension Matrix: an array with two or more dimensions Size: specified by the number of rows and the number of columns in the array, with the number of rows mentioned first. >> [1 2 3 4 5 6 7 8 9]; % create an arrays with 9 elements >> [1,2,3;4,5,6;7,8,9] % create a 3×3 matrix ans = 1 2 3 4 5 6 7 8 9 >> [1 2 3 4 5 6 7 8 9]
MATLAB Lecture chool of mathematical Sciences xiamen Univers http://gdjpkc.xmu.edu.cn =147 258 369 MATLAB -Getting Started - Matrices and Arrays- Matrices and Magic Square → Entering Matrices 1. Initializing Variables in MaTLaB(变量) Var=expresston Assign data to the variable in an assignment statement(赋值语句) Input data into the variable from the keyboard Read data from a file Some rules on variable t Begin with a letter t Followed by any combination of letters, numbers and the underscore()character t Only the first 31 characters are significant Good programming practice Easy-to-remember( e.g. name, NAME, Name are different Use only lowercase letter( e.g. name is the best > aa =[1 2 34567891: create an arrays with 9 elements >>a=[1 23: 456:7891 create a 3X3 matrix a 6 >>b=[1,2,3;4,5,6:7,8,9]; >> [1 >>d=[a(1,2),7+3 MATLAB -Getting Started Matrices and Arrays Expressions - Variables 2. Initializing with Shortcut Expressions- by Colon Operator(冒号运算) First: iner: last > al=1: 9:8 obtain a row vector containing the integers from 1 >>a2=1:2:10; >>a3=10:-3:0 pi:0.3 > subal a(1: 2, 3)i obtain a matrix Al, which is a partition of matrix a with the first 2 elements of the 3rd column of a >>suba2=a(1:2,:); >>suba3=a(:;2) >>c=a(:[2,3,1])for each of the rows of matrix A, reorder
MATLAB Lecture 1 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec13 ans = 1 2 3 4 5 6 7 8 9 MATLAB → Getting Started → Matrices and Arrays → Matrices and Magic Square → Entering Matrices 1. Initializing Variables in MATLAB (变量) Var = expression ÿ Assign data to the variable in an assignment statement(赋值语句). ÿ Input data into the variable from the keyboard. ÿ Read data from a file. Some rules on variable: ² Begin with a letter ² Followed by any combination of letters, numbers and the underscore ( _ ) character ² Only the first 31 characters are significant ² Case sensitive Good programming practice Easytoremember ( e.g. name, NAME, Name are different ) Use only lowercase letter ( e.g. name is the best ) >> aa = [1 2 3 4 5 6 7 8 9]; % create an arrays with 9 elements >> a = [1 2 3; 4 5 6; 7 8 9] % create a 3×3 matrix a = 1 2 3 4 5 6 7 8 9 >> b = [1,2,3; 4,5,6; 7,8,9]; >> c = [1 2; 3 4]; >> d = [a(1,2), 7+3] MATLAB → Getting Started → Matrices and Arrays → Expressions → Variables 2. Initializing with Shortcut Expressions by Colon Operator ( 冒号运算 ) First : incr : last >> a1 = 1:9; % obtain a row vector containing the integers from 1 … to 9 >> a2 = 1:2:10; >> a3 = 10:3:0; >> a4 = pi:0.3:pi; >> suba1 = a(1:2,3); %obtain a matrix subA1, which is a partition … of matrix A with the first 2 elements of the 3rd column of A >> suba2 = a(1:2,:); >> suba3 = a(:,2); >> C = a(:,[2,3,1]); %for each of the rows of matrix A, reorder …
MATLAB Lecture hoolofMathematicalSciencesXiamenUniversityhttp://gdjpkc.xmu.edu.cn ts in the order 2 >>D=a(:,end); MATLAB→ Getting Started→ Matrices and Arrays→ Matrices and Magic Square The colon o 3. Array and Matrix Operations Operation MATLAB FormComments Array Addition a +b Array addition and matrix addition are identical Array Subtraction a-b Array subtraction and matrix subtration are identical Element-by-el multiplication array must be the same shape, or one of them must be a Matrix Matrix multiplication of a and b. The number of Multiplication columns in a must equal to the number of rows in b. or one of them must be a scal Righ / b a(ij)/b(1j) Division Both arrays must be the same shape, or one of them must be a scalar Left a b Element-by-element division of a and b b (ij)/b(i,j) Division Both arrays must be the same shape, or one of them nust be a scalar Matrix ght a/b DiⅤ ISion Matrix division defined by ab-I Matrix Left a\b Division Matrix division defined by a b Array Element-by-element exponentiation of a and b (i, j) u, Both arrays must be the same shape, or one of them must be a scalar > al eye[2, 2]8 obtain an identity matrix A1= 0 >>a2=[2,2;1,3]; >>al*a2 8 Matrix multiplication ans >>al.*a2 Element-by-element multiplication ans >>b1=[123];b2=[2311
MATLAB Lecture 1 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec14 the elements in the order 2, 3, 1 >> D = a(:,end); MATLAB → Getting Started → Matrices and Arrays → Matrices and Magic Square → The Colon Operator 3. Array and Matrix Operations Operation MATLAB Form Comments Array Addition a + b Array addition and matrix addition are identical. Array Subtraction a b Array subtraction and matrix subtration are identical. Array Multiplication a .* b Elementbyelement multiplication of a and b. Both array must be the same shape, or one of them must be a scalar. Matrix Multiplication a * b Matrix multiplication of a and b. The number of columns in a must equal to the number of rows in b, or one of them must be a scalar.. Array Right Division a ./ b Elementbyelement division of a and b: a(i,j)/b(i,j). Both arrays must be the same shape, or one of them must be a scalar. Array Left Division a .\ b Elementbyelement division of a and b:b(i,j)/b(i,j). Both arrays must be the same shape, or one of them must be a scalar. Matrix Right Division a / b Matrix division defined by 1 ab- . Matrix Left Division a \ b Matrix division defined by 1 a b - . Array Exponentiation a .^ a Elementbyelement exponentiation of a and b: ( , ) ( , ) b i j a i j . Both arrays must be the same shape, or one of them must be a scalar. >> a1 = eye[2,2] % obtain an identity matrix A1 = 1 0 0 1 >> a2 = [2, 2; 1, 3]; >> a1*a2 % Matrix multiplication ans = 2 2 1 3 >> a1.*a2 % Elementbyelement multiplication ans = 2 0 0 3 >> b1=[1 2 3]; b2=[2 3 1];
MATLAB Lecture hool of mathematical Sciences xiamen Univers tp: /gdjpkc. xmu. edu.cn >>b1*b2 ??? Error using == times Inner matrix dimensions must agree a 8 al(a2) 0.7500-0.5000 0.25000.5000 >>a1\a28(a1)a2 ans 4. *Initializing with Built-in Functions MATLAB provides four functions that generate basic matrices All zeros nes All ones ra Uniformly distributed random elements rand Normally distributed random element s sin, Cos, tan, cot, sec sC rt, exp, log, log10, log2 abs, sign, round, fix, floor, ceil, sum, prod, max, min, mean sort, rand, randn eye, diag, triu, tril
MATLAB Lecture 1 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec15 >> b1*b2 ??? Error using ==> mtimes Inner matrix dimensions must agree. >> b1.*b2 ans = 2 6 3 >> a1/a2 % 1 a1(a2)- ans = 0.7500 0.5000 0.2500 0.5000 >> a1\a2 % 1 (a1) a2 - ans = 2 2 1 3 4. *Initializing with Builtin Functions clock, date; sin, cos, tan, cot, sec, csc, asin, acos; x.^a, sqrt, exp, log, log10, log2; abs, sign, round, fix, floor, ceil, sum, prod, max, min, mean; sort, rand, randn; eye, diag, triu, tril
MATLAB Lecture SchoolofMathematicalSCiencesXiamenUniversityhttp:/gdjpkc.xmu.edu.cn Several special functions provide values of useful constants 3.14159265 Imaginary unit S 52 Float t relati realmin Smallest floating-point number. 21022 almax 1023 Largest floating-point number, 9 Infinity Not-a-numbe MATLAB - Mathematics - Matrices and Linear Algebra Matrices in maTlaB 5.*Initializing Variables with Keyboard Input my_val= input (Enter an input value: ) > age s input ('Please enter your age: ')i Please enter your age: 2 > profession input ('Please enter your profession:','s) Please enter your profession: teacher Clearing 8 Clearing the Command Window. This does not clear the workspace, but only clears the view. Afterwards, you still > clear Remove items from workspace, freeing up system memory Lec1-6
MATLAB Lecture 1 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec16 MATLAB → Mathematics → Matrices and Linear Algebra → Matrices in MATLAB 5. *Initializing Variables with Keyboard Input my_val = input ( ‘Enter an input value:’ ); >> age = input (‘Please enter your age: ’); Please enter your age: 20 >> profession = input (‘Please enter your profession: ’, ’s’); Please enter your profession: teacher l Clearing >> clc % Clearing the Command Window. This does not clear the workspace, but only clears the view. Afterwards, you still can use the up arrow key to recall previous functions. >> clear % Remove items from workspace, freeing up system memory