1、天 津 科 技 大 学 数 学 系 谢中华 E-mail: MATLAB从零到进阶,MATLAB基本操作,主要内容,变量的定义与数据类型常用函数数组运算MATLAB常用标点符号MATLAB常用快捷键和快捷命令,第一节 变量的定义与数据类型,1. 变量命名规则,一、 变量的定义与赋值,可由任意的字母、数字或下划线组成,但必须以字母打头;变量名区分字母大小写;变量名最多不超过63个字符(MATLAB6.5及以上版本)。,2. 特殊变量与常量列表, x = 1 x =1 y = 1+2+sqrt(9) y =6 z = Hellow World ! z = Hellow World !,3. 赋值
2、语句,二、数据类型,MATLAB中有15种基本的数据类型,有逻辑型、字符型、整型、浮点型、结构数组、元胞数组以及函数句柄等。其中整型又分为有符号整型和无符号整型,8位整型、16位整型、32位整型和64位整型,浮点型又分为单精度浮点型和双精度浮点型。具体可以通过MATLAB中自带的isa函数查看。,三、数据输出格式,MATLAB中数值型数据的输出格式可以通过 format 命令指定,续表:,第二节 常用函数,常用函数列表:, x = 1 -1.65 2.2 -3.1; y1 = abs(x) y1 =1.0000 1.6500 2.2000 3.1000 y2 = sin(x) y2 =0.84
3、15 -0.9969 0.8085 -0.0416 y3 = round(x) y3 =1 -2 2 -3 y4 = floor(x) y4 =1 -2 2 -4,【例2.2-1】常用函数的用法举例, y5 = ceil(x) y5 =1 -1 3 -3 y6 = min(x) y6 =-3.1000 y7 = mean(x) y7 =-0.3875 y8 = range(x) y8 =5.3000 y9 = sign(x) y9 = 1 -1 1 -1,第三节 数组运算,一、矩阵的定义,【例2.3-1】按行方式输入矩阵元素。, x = 1, 2, 3;4 5 6;7 8, 9 x =1 2
4、34 5 67 8 9 y = 1 2 34 5 67 8 9 y =1 2 34 5 67 8 9,【例2.3-2】通过冒号运算符构造向量和矩阵。, x = 1:10 x =1 2 3 4 5 6 7 8 9 10 y = 1:2:10 y =1 3 5 7 9 z = 1:3; 4:6; 7:9 z =1 2 34 5 67 8 9,【例2.3-3】linspace函数用来生成等间隔向量。, x = linspace(1, 10, 10) x =1 2 3 4 5 6 7 8 9 10,调用格式: x = linspace(初值, 终值, 向量长度),【例2.3-4】利用size函数返回矩
5、阵的行数和列数。, x = 1 2 3; 4 5 6 x =1 2 34 5 6 size(x) ans =2 3 m, n = size(x) m =2 n =3,【例2.3-5】利用行标、列标和冒号运算符提取矩阵元素。, x = 1 2 3; 4 5 6; 7 8 9 x =1 2 34 5 67 8 9 y1 = x(1, 2) y1 =2 y2 = x(2:3, 1:2) y2 =4 57 8, y3 = x(:, 1:2) y3 =1 24 57 8 y4 = x(1, :) y4 =1 2 3 y5 = x(:) y5 =1 4 7 2 5 8 3 6 9 y6 = x(3:6)
6、y6 =7 2 5 8,【例2.3-6】通过拼凑和变形来定义新的矩阵。, x1 = 1 2 3; x2 = 4 5 6; x = x1; x2 x =1 2 34 5 6 y = reshape(x, 3, 2) y =1 54 32 6 z = repmat(x, 2, 2) z =1 2 3 1 2 34 5 6 4 5 61 2 3 1 2 34 5 6 4 5 6,【例2.3-7】定义字符型矩阵。, x = abc; def; ghi x = abc def ghi size(x) ans =3 3,【例2. 3-8】定义复数矩阵。, x = 2i+5 x =5.0000 + 2.00
7、00i y = 1 2 3; 4 5 6*i+7 y =7.0000 + 1.0000i 7.0000 + 2.0000i 7.0000 + 3.0000i7.0000 + 4.0000i 7.0000 + 5.0000i 7.0000 + 6.0000i a = 1 2; 3 4; b = 5 6; 7 8; c = complex(a,b) c =1.0000 + 5.0000i 2.0000 + 6.0000i3.0000 + 7.0000i 4.0000 + 8.0000i,【例2. 3-9】定义符号矩阵。, syms a b c d x = a b; c d x = a, b c,
8、d y = 1 2 3; 4 5 6; y = sym(y) y = 1, 2, 3 4, 5, 6,二、特殊矩阵,零矩阵:zeros一矩阵:ones单位阵:eye对角阵:diag随机阵:rand魔方阵:magic,【例2.3-10】生成特殊矩阵。, A = zeros(3) B = ones(3,5) C = eye(3,5) D = diag(1 2 3) E = diag(D) F = rand(3) G = magic(3),三、高维数组,【例2.3-11】通过直接赋值的方式定义3维数组。, x(1:2, 1:2, 1)=1 2; 3 4; x(1:2, 1:2, 2)=5 6; 7
9、8; x(:,:,1) =1 23 4 x(:,:,2) =5 67 8,【例2. 3-12】利用cat函数定义3维数组。, A1 = 1 2; 3 4; A2 = 5 6; 7 8; A = cat(3, A1, A2) A(:,:,1) =1 23 4 A(:,:,2) =5 67 8,【例2. 3-13】利用reshape函数定义3维数组。, x = reshape(1:12, 2, 2, 3) x(:,:,1) =1 32 4 x(:,:,2) =5 76 8 x(:,:,3) =9 1110 12,【例2.3-14】利用repmat函数定义3维数组。, x = repmat(1 2;
10、 3 4, 1 1 2) x(:,:,1) =1 23 4 x(:,:,2) =1 23 4,四、定义结构体数组,【例2.3-15】直接赋值定义结构体数组。, struct1(1).name = xiezhh; struct1(2).name = yanlih; struct1(1).age = 31; struct1(2).age = 32; struct1 struct1 = 1x2 struct array with fields:name age,【例2.3-16】 利用struct函数定义结构体数组。, struct2 = struct(name, xiezhh, yanlih, a
11、ge,31, 32) struct2 = 1x2 struct array with fields:nameage struct2(1).name ans =xiezhh,调用格式: s = struct(field1, values1, field2, values2, ) s = struct(field1, , field2, , ),五、定义元胞数组,【例2.3-17】直接赋值定义元胞数组。, c1 = 1 2; 3 4, xiezhh, 5 6 7, I LOVE MATLAB c1 = 2x2 double xiezhh 1x3 double I LOVE MATLAB,【例2.3
12、-18】 利用cell函数定义元胞数组。, c2 = cell(2,4) c2 = c22, 3 = 1 2 3 c2 = 1x3 double ,调用格式: c = cell(n) c = cell(m, n) c = cell(m, n) c = cell(m, n, p,) c = cell(m n p ) c = cell(size(A),【例2.3-19】元胞数组的访问。,访问元胞数组C的第i行第j列的元胞,用命令C(i, j),注意用的是圆括号;访问元胞数组C的第i行第j列的元胞里的元素,用命令Ci, j,注意用的是花括号。celldisp函数可以显示元胞数组里的所有内容。, c
13、= 1 2, xie, xiezhh; MATLAB, 3 4; 5 6, I LOVE MATLAB c = 1x2 double xie xiezhh MATLAB 2x2 double I LOVE MATLAB c(2, 2) ans = 2x2 double c2, 2 ans =3 45 6, c = 1 2, xiezhh; MATLAB, 3 4; 5 6; celldisp(c) c1,1 =1 2 c2,1 =MATLAB c1,2 =xiezhh c2,2 =3 45 6,六、几种数组的转换,mat2cell,将矩阵分块,转为元胞数组cell2mat,将元胞数组转为矩阵n
14、um2cell ,将数值型数组转为元胞数组cell2struct,将元胞数组转为结构数组struct2cell,将结构数组转为元胞数组num2str,将数值型数组转为字符型数组str2num,将字符型数组转为数值型数组,【例2.3-20】数组转换函数示例。, A1 = rand(60,50); B1 = mat2cell(A1, 10 20 30, 25 25) C1 = cell2mat(B1); isequal(A1,C1) A2 = 1 2 3 4;5 6 7 8;9 10 11 12; B2 = num2cell(A2) C = Heping, Tianjin, 35; Xiezhh,
15、 Xingyang, 30 fields = Name, Address, Age; S = cell2struct(C, fields, 2) CS = struct2cell(S) isequal(C,CS),七、矩阵的算术运算,1. 矩阵的加减,【例2.3-21】矩阵的加减运算。, A = 1 2; 3 4; B = 5 6; 7 8; C = A+B C =6 810 12 D = A-B D =-4 -4-4 -4,2. 矩阵的乘法,【例2.3-22】矩阵的乘法。, A = 1 2 3; 4 5 6; B = 1 1 1 1; 2 2 2 2; 3 3 3 3; C = A*B C
16、=14 14 14 14 32 32 32 32 D = 1 1 1; 2 2 2; E = A.*D E =1 2 38 10 12,3. 矩阵的除法,矩阵的除法包括左除(AB)、右除(A/B)和点除(A./B)三种。一般情况下,x = Ab是方程组A*x = b的解,而x = b/A是方程组x*A = b的解,x = A./B表示同型矩阵A和B对应元素相除,【例2.3-23】矩阵的除法。, A = 2 3 8; 1 -2 -4; -5 3 1; b = -5; 3; 2; x = Ab x =13-2 B = A; C = A./B C =1 1 11 1 11 1 1,4. 矩阵的乘方(
17、)与点乘方(. ),矩阵的乘方要求矩阵必须是方阵,有以下3种情况: (1)矩阵A为方阵,x为正整数,A x表示矩阵A自乘x次; (2)矩阵A为方阵,x为负整数,A x表示矩阵A-1自乘x次; (3)矩阵A为方阵,x为分数,例如x = m/n,A x表示矩阵A先自乘m次,然后对结果矩阵里的每一个元素开n次方。矩阵的点乘方不要求矩阵为方阵,有以下2种情况: (1)A为矩阵,x为标量,A. x表示对矩阵A中的每一个元素求x次方; (2)A和x为同型矩阵,A. x表示对矩阵A中的每一个元素求x中对应元素次方。,【例2.3-24】矩阵乘方与点乘方。, A = 1 2; 3 4; B = A 2 B =7
18、 1015 22 C = A . 2 C =1 49 16 D = A . A D =1 427 256,八、矩阵的关系运算,矩阵的关系运算是通过比较两个同型矩阵的对应元素的大小关系,或者比较一个矩阵的各元素与某一标量之间的大小关系,返回一个逻辑矩阵(1表示真,0表示假)。关系运算的运算符有: (大于)、= (大于或等于)、= = (等于)、= (不等于)6种, A = 1 2; 3 4; B = 2 2; 2 2; C1 = A B C1 =0 01 1 C2 = A = B C2 =1 01 1 C3 = A =2 C3 =0 11 1,九、矩阵的逻辑运算,矩阵的逻辑运算包括:逻辑“或”运
19、算,运算符为“|”. A | B表示同型矩阵A和B的或运算,若A和B的对应元素至少有一个非0,则相应的结果元素值为1,否则为0;逻辑“与”运算,运算符为“&”. A & B表示同型矩阵A和B的与运算,若A和B的对应元素均非0,则相应的结果元素值为1,否则为0;逻辑“非”运算,运算符为“”. A表示矩阵A的非运算,若A的元素值为0,则相应的结果元素值为1,否则为0;,逻辑“异或”运算。xor(A, B)表示同型矩阵A和B的异或运算,若A和B的对应元素均为0或均非0,则相应的结果元素值为0,否则为1. 先决与运算,运算符“&”. A & B 表示当A为真时,才执行A和B的逻辑与运算先决或运算,运算
20、符“|”. A | B 表示当A为真时,不用再执行A和B的逻辑或运算,【例2.3-26】矩阵的逻辑运算。, A = 0 0 1 2; B = 0 -2 0 1; C1 = A | B C1 =0 1 1 1 C2 = A & B C2 =0 0 0 1 C3 = A C3 =1 1 0 0 C4 = xor(A, B) C4 =0 1 1 0,十、运算符的优先级,注:级别1优先级最高,11级别最低,十一、矩阵的其他常用运算,1. 矩阵的转置,【例2.3-27】矩阵的转置。, A = 1 2 3; 4 5 6; 7 8 9 A =1 2 34 5 67 8 9 B = A B =1 4 72 5
21、 83 6 9,2. 矩阵的翻转,【例2.3-28】矩阵的翻转。, A = 1 2 3; 4 5 6; 7 8 9; B1 = flipud(A) B1 =7 8 94 5 61 2 3 B2 = fliplr(A) B2 =3 2 16 5 49 8 7 B3 = rot90(A) B3 =3 6 92 5 81 4 7,3. 方阵的行列式,【例2.3-29】方阵的行列式。, A = 1 2; 3 4; d1 = det(A) d1 =-2 syms a b c d B = a b; c d; d2 = det(B) d2 =a*d-b*c,4. 逆矩阵与广义伪逆矩阵,【例2.3-30】逆矩
22、阵与广义伪逆矩阵。, A = 1 2; 3 4; Ai = inv(A) Ai =-2.0000 1.00001.5000 -0.5000 syms a b c d B = a b; c d; Bi = inv(B) Bi = d/(a*d-b*c), -b/(a*d-b*c) -c/(a*d-b*c), a/(a*d-b*c),2019/5/16, C = 1 2 3; 4 5 6; Cpi = pinv(C) Cpi =-0.9444 0.4444-0.1111 0.11110.7222 -0.2222 D = C * Cpi * C D =1.0000 2.0000 3.00004.00
23、00 5.0000 6.0000,2019/5/16,5. 方阵的特征值与特征向量,【例2.3-31】方阵的特征值与特征向量。, A = 5 0 4; 3 1 6; 0 2 3; d = eig(A) d =-1.00003.00007.0000 V, D = eig(A) V =-0.2857 0.8944 0.6667-0.8571 0.0000 0.66670.4286 -0.4472 0.3333 D =-1.0000 0 00 3.0000 00 0 7.0000,2019/5/16, Vs, Ds = eig(sym(A) Vs = 2, 1, -2 2, 3, 0 1, -3/2
24、, 1 Ds = 7, 0, 0 0, -1, 0 0, 0, 3,2019/5/16,6. 矩阵的迹和矩阵的秩,【例2.3-32】矩阵的迹和矩阵的秩。, A = 1 2 3; 4 5 6; 7 8 9; t = trace(A) t =15 r = rank(A) r =2,第四节 MATLAB常用标点符号,2019/5/16,MATLAB常用标点符号,第五节 MATLAB常用快捷键和快捷命令,2019/5/16,一、 MATLAB常用快捷键,2019/5/16,二、 MATLAB常用快捷命令,2019/5/16,【例2.5-1】MATLAB常用快捷命令举例。, A = 1 2 3;4 5 6;7 8 9; B = 100; Str = Hellow World !; C = cell(2,3); S = struct(name, heping,xiezhh, age,30, 32); syms a b c d D = a b;c d; whos save xiezhh.mat A B Str clear A B Str load xiezhh.mat which sin open sqrt,