版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Matlab 笔记 基础操作 1,基础命令 Cd+确定路径 :转变当前目录 Pwd :查看当前目录的确定路径 doc addpath:显现帮忙菜 单中关于 addpath 的学问 help addpath :命令窗口显现 addpath 的介绍 lookfor :关键词搜寻命令, lookfor color 与 color 有关的函数 注:搜寻时有时会显现大量信息,按 ctrl+C 可终止搜寻,或者在使用该命令 前先输入 more on 来启动分屏显示功能,输入 more off 回车,可以关闭分屏显 示功能; What : :查看当前目录下的文件那些和 matlab 有关 What gene
2、ral:列出 matlab 的基础函数名 WhichDemo:可以很便利地给出某个函数的确定路径 :可以进入 matlab 自带的演示教程 回车 home 回车 将光标置于当前窗口的左上方 1Edit Edit or create file Alternatives As an alternative to the edit function, select File New or Open in the MATLAB desktop or any desktop tool. Syntax edit edit edit edit fun1 fun2 fun3 . edit classname/
3、fun edit private/fun edit classname/private/fun edit +packagename/classname/fun editmy file.m Description edit opens a new editor window. filespecification can include a partial path, complete path, relative path, or no path. Be aware of the following: If you do not specify a path, the current folde
4、r is the default. If you specify a path, the folder must exist; otherwise MATLAB returns an error. If you specify a path and the folder exits, but the specified file does not, a prompt opens such as shown in the following image: To create a blank file named fun.m in the specified folder, click Yes.
5、To suppress the prompt, select Donot show this prompt again. To reinstate 第 1 页,共 88 页the prompt after suppressing it, open the Preferences dialog box by selecting File Preferences General Confirmation Dialogs and then selecting Prompt when editing files that do not exist in the pane on the right. e
6、dit file.ext opens the specified file. edit fun1 fun2 fun3 . opens fun1.m, fun2.m, fun3.m, and so on, in the default editor. edit classname/fun, or edit private/fun, or edit classname/private/fun opens a method, private function, or private method for the namedclass. edit +packagename/classname/fun
7、opens a method for the namedclass in the named package. editmy file.m opens the file my file.m in the default editor. This form of the edit function is useful when a file name contains a space; you cannot use the command form in such a case. 2,命令输入的几个有用方法 a,利用 tab 按键 输入命令的前几个字符,接着连按两下 tab 键,显现函数列表 b
8、,利用键盘上的上下方向键,调出历史命令 c,当 matlab 按次序执行很多命令时,可将要运行的大量命令统一存放在一个文本文件里, 扩展名为 .m,然后在命令窗口键入这个文件名,回车, 3,matlab 保留常数 Ans eps :当前运算机的零阈值 matlab 就会一次性地执行这些命令 i 和 f :如 i 和 f 值不被改写, 就它们代表虚数单位, 使用前, 应确定它们没有被改写; 复原变量: i=sqrt (-1); Inf :无穷大,负无穷用 -inf 表示 NaN:非数( not a number ) Nargin:函数输入的变量的实际个数 Nargout :函数输出变量的实际个数
9、 Pi :圆周率 4,变量命名规章 首个字符必需是字母,并且后面字符不能包含空格,标点和括号; Matlab 中大小写字符 表示的含义不同 5,数据类型结构 利用 help datatype 查看全部的数据类型基本类型 常用的: 数值型:双精度,浮点数,其 matlab 表示为 double 规律型:只包含 1,0 ; 1 为 true , 0 为 false 字符型:赋值时,字符串是用单引号括起来的 单元数据 clear all % 清除运算机内存所储存的变量和函数 who % 查看当前内存里的变量名 whos % 查看当前内存里变量的详细说明 clc % 清屏 注:命令行结尾加上一个分号表
10、示不屏显结果,只运行命令; 第 2 页,共 88 页矩阵 1, 基本规章 a, 矩阵元素必需在中括号内 b, 同行元素之间用空格或逗号隔开 c, 行与行之间可以用分号或回车隔开 d, 假如矩阵的某行过长,可以在续行符 后另起一行接着输入 2, 矩阵函数 Ones :生成元素全为 1 的矩阵, onesn 将生成 n*n 的矩阵 Zeros :生成元素全为 0 的矩阵 Nan :生成元素全为 nan 的矩阵 区间均匀Rand :生成在 0,1 分布的随机矩阵 Randn :生成均值为 0,方差为 1 的标注正态分布随机矩阵 Eye :生成单位矩阵,主对角线元素全为 1Linspace :生成等间
11、隔的行向量,其调用格式为: linspacea,b,n ,其中 a 和 b 是生成 向量的第一个和最终一个元素, n 是元素总数 Magic :魔方矩阵 3,矩阵运算 + - * / 右除 左除 乘方 转置 a,点运算 点运算符号有 .* ./ . . 两矩阵进行点运算是指对它们的对应元素进行相关运算,要求两个矩阵的维数相同 b,关系运算 = b=-3:2 % 生成间隔为 1 的向量 c=130:-2.4:115 % 中间可以指定任意间隔 d=a:,3 % 提取矩阵 a 的第 3 列元素,并转置后赋值给变量 dE,矩阵的维数 a= 1 2 3 4 7 8 9 10 3 4 6 9 5 7 10
12、 11 s1,s2=sizea %将矩阵 a 的每一维的长度赋值给 s1 和 s2 S1=4 s2=4 向量 b 的长度 b=-3 -2 -1 0 1 2 b1=lengthb %第 3 页,共 88 页b1= 6 ndimsa%直接得到 a 的维数 Ans=2 f ,空矩阵的应用 空矩阵的表示 a3,:= %删除矩阵 a 的第 3 行 h,矩阵的翻转 1 利用撇号可以实现矩阵的行列互换 2fliplra % 将矩阵 a 左右翻转,第一列变成最终一列,以此类推 flipuda % 将矩阵 a 上下翻转,第一行变成最终一行,以此类推 rot90a % 将矩阵 a 逆时针旋转 90 度 Rot90
13、a,k : 将矩阵 a 逆时针旋转 k 倍的 90 度, k 为 1 可省略 g,矩阵的拼接 b=a ones3,6*2;nansizea a-1 k,基本函数操作 help elfun 和 help matfun 可以查看基本的数学函数和线性代数运算函数列表 suma % 对矩阵 a 的每一列求和 suma:; % 对矩阵 a 的全部元素求和 a: 将矩阵 a 变成向量 a 补充: type + 文件名 % 查看当期文件目录下的内容 利用 mat 文件可以将当前 matlab 工作空间中的一些有用的变量长期地保留下来,扩展名 为.mat , mat 文件生成和装入用 save 和 load
14、命令完成 常用格式: Save 文件名 变量名表 -append-ascii load 文件名 变量名表 -ascii save wps % 储存为 文件 lsload wps %装入 mat 文件 save wps x y %只储存 x y 变量 load wps x %只装入 x 变量 help tic/toc %时间机器 取整函数 : fix floor ceil round fix1.2=1 fix2.3=2 靠近 0 取整 floor1.2=1floor-1.2=2 靠近负无穷取整 ceil1.2=2 ceil-1.2=-1 靠近正无穷取整 round1.2=1 round1.6=2
15、 四舍五入 a= 1 2 3;4 5 6 aend,end ans=6 aend-1,endans=2 Matlab 指令及函数总结 第 4 页,共 88 页logical Convert numeric values to logical Syntax K = logicalA K = logicalA returns an array that can be used for logical indexing or logical tests. Examples the statement B = logicaleye3 returns Given A = 1 2 3; 4 5 6; 7
16、8 9, a logical array B = 100indexing that returns As diagonal elements: 010001which can be used in logical AB ans = 1 5 9 However, attempting to index into A using the numeric array eye3 results in: Aeye3 . Subscript indices must either be real positive integers or logicals. ndims Number of array di
17、mensions Syntax n = ndimsA Description n = ndimsA returns the number of dimensions in the array A. The number of dimensions in an array is always greater than or equal to 2. Trailing singleton dimensions are ignored. A singleton dimension is any dimension for which sizeA,dim = 1. Algorithms ndimsx i
18、s lengthsizex. size Array dimensions Syntax d = sizeX m,n = sizeX m = sizeX,dim d1,d2,d3,.,dn = sizeX, Description 第 5 页,共 88 页d = sizeX returns the sizes of each dimension of array X in a vector d with ndimsX elements. If X is a scalar, which MATLABsoftware regards as a 1-by-1 array, sizeX returns
19、the vector 1 1. m,n = sizeX returns the size of matrix X in separate variables m and n. m= sizeX,dim returns the size of the dimension of X specified by scalar dim.d1,d2,d3,.,dn = sizeX, for n 1, returns the sizes of the dimensions of the array X in the variables d1,d2,d3,.,dn, provided the number o
20、f output arguments n equals ndimsX. If n does not equal ndimsX, the following exceptions hold: n ndimsX size returns ones in the extra variables, that is, those corresponding to ndimsX+1 through n. Examples Example 1 The size of the second dimension of rand2,3,4 is 3. m = sizerand2,3,4,2 m = 3 Here
21、the size is output as a single vector. d = sizerand2,3,4 d = 234Here the size of each dimension is assigned to a separate variable. m,n,p = sizerand2,3,4 m = 2n = 3p = 4 Example 2 If X = ones3,4,5, then d1,d2,d3 = sizeX d1 = d2 = d3 = 第 6 页,共 88 页3 4 5But when the number of output variables is less
22、than ndimsX: d1,d2 = sizeX d1 = d2 = 3 20 The extra dimensions are collapsed into a single product. If n ndimsX, the extra variables all represent singleton dimensions: d1,d2,d3,d4,d5,d6 = sizeX d1 = d2 = d3 = 5d6 = 134d4 = d5 = 1length 1Length of vector or largest array dimension Syntax numberOfEle
23、ments = lengtharray Description numberOfElements = lengtharray finds the number of elements along the largest dimension of an array. array is an array of any MATLAB data type and any valid dimensions. numberOfElements is a whole number of the MATLAB double class. For nonempty arrays, numberOfElement
24、s is equivalent to maxsizearray. For empty arrays, numberOfElements is zero. Examples Create a 1-by-8 array X and use length to find the number of elements inthe second largest dimension: X = 5, 3.4, 72, 28/4, 3.61, 17 94 89; lengthX ans = 8Create a 4-dimensional array Y in which the third dimension
25、 is the largest. Use length to find the number of elements in that dimension: Y = rand2, 5, 17, 13; lengthY ans = 17 第 7 页,共 88 页Create a struct array S with character and numeric fields of different lengths. Use the structfun function to apply length to each field of S: S = structf1, Name:, f2, Cha
26、rlie, . f3, DOB:, f4, 1917 S = f1: Name: f2: Charlief3: DOB: f4: 1917 structfunfieldlengthfield, S ans = 5741numel Number of elements in array or subscripted array expressionSyntax n = numelA n = numelA, index1, index2, . indexn Description n = numelA returns the number of elements, n, in array A. n
27、 = numelA, index1, index2, . indexn returns the number of subscripted elements, n, in Aindex1, index2, ., indexn. To handle the variable number of arguments, numel is typically written with the header function n = numelA, varargin, where varargin is a cell array with elements index1, index2, . index
28、n. The MATLABsoftware implicitly calls the numel built-in function whenever an expression generates a comma-separated list. This includes brace indexing i.e., Aindex1,index2,.,indexN, and dot indexing i.e., A.fieldname.Tips It is important to note the significance of numel with regards to the overlo
29、aded subsref and subsasgn functions. In the case of the overloaded subsref function for brace and dot indexing as described in the last paragraph, numel is used to compute the number of expected outputs nargout returned from subsref. For the overloaded subsasgn function, numel is used to compute the
30、 number of expected inputs nargin to be assigned using subsasgn. The nargin value for the overloaded subsasgn function is the value returned by numel plus 2 one for the variable being assigned to, and one for the structure array of subscripts. As a class designer, you must ensure that the value of n
31、 returned by the 第 8 页,共 88 页built-in numel function is consistent with the class design for that object. If n is different from either the nargout for the overloaded subsref function or the nargin for the overloaded subsasgn function, then you need to overload numel to return a value of n that is c
32、onsistent with the class subsref and subsasgn functions. Otherwise, MATLAB produces errors when calling these functions. Examples Create a 4-by-4-by-2 matrix. numel counts 32 elements in the matrix. a = magic4; a:,:,2 = a a:,:,1 = 16 2313 511 10 897612 414 15 1a:,:,2 = 16 594211 714 310 615 13 812 1
33、numela ans = 32 reshapeReshape arraySyntax B = reshapeA,m,n B = reshapeA,m,n,p,. B = reshapeA,m n p . B = reshapeA,.,. B = reshapeA,siz Description B = reshapeA,m,n returns the m-by- n matrix B whose elements are taken column-wise from A. An error results if A does not have m*n elements. B = reshape
34、A,m,n,p,. or B = reshapeA,m array with the same elements as A n p . returns an n-dimensional but reshaped to have the size m-by- n-by- p-by- . . The product of the specified dimensions, m*n*p* ., must be the same as prodsizeA . B = reshapeA,.,. calculates the length of the dimension represented by t
35、he placeholder , such that the product of the dimensions equals prodsizeA . The value of prodsizeA must be evenly divisible by the 第 9 页,共 88 页product of the specified dimensions. You can use only one occurrence of . . B = reshapeA,siz returns an n-dimensional array with the same elements as A, but
36、reshaped to siz , a vector representing the dimensions of the reshaped array. The quantity prodsiz must be the same as prodsizeA Examples Reshape a 3-by- 4matrix into a 2 -by- 6 matrix. A = 10 14725811 36912 B = reshapeA,2,6 B = 1357911 12 246810 B = reshapeA,2, B = 1357911 246810 12 permute Rearran
37、ge dimensions of N-D arraySyntax B = permuteA,order Description B = permuteA,order rearranges the dimensions of A so that they are in the order specified by the vector order . B has the same values of A but the order of the subscripts needed to access any particular element is rearranged as specifie
38、d by order . All the elements of order must be unique. Tips permute and ipermute are a generalization of transpose . for multidimensional arrays. Examples Given any matrix A, the statement permuteA,2 1 is the same asA. . For example: A = 1 2; 3 4; permuteA,2 1 ans = 1 32 4The following code permutes
39、 a three-dimensional array: 第 10 页,共 88 页X = rand12,13,14; Y = permuteX,2 3 1; sizeY ans = 13 14 12 repmat Replicate and tile arraySyntax B = repmatA,m,n B = repmatA,m n B = repmatA,m n p. Description B = repmatA,m,n creates a large matrix B consisting of an m-by- n tiling of copies of A. The size o
40、f B is sizeA,1*m , sizeA,2*n . The statement repmatA,n creates an n-by- n tiling. B = repmatA,m n accomplishes the same result as repmatA,m,n . B = repmatA,m n p. produces a multidimensional array B composed of copies of A. The size of B is sizeA,1*m , sizeA,2*n , sizeA,3*p , . . Tips repmatA,m,n ,
41、when A is a scalar, produces an m-by- n matrix filled with As value and having As class . For certain values, you can achieve the same results using other functions, as shown by the following examples: repmatNaN,m,n returns the same result as NaNm,n . . . repmatsingleinf,m,n is the same as infm,n,si
42、ngle repmatint80,m,nis the same as zerosm,n,int8 . repmatuint321,m,n is the same as onesm,n,uint32 repmateps,m,n is the same as epsonesm,n . ExamplesExample 1 In this example, repmat replicates 12 copies of the second-order identity matrix, resulting in a checkerboard pattern. B = repmateye2,3,4 B =
43、 1 0 1 0 1 0 1 00 1 0 1 0 1 0 11 0 1 0 1 0 1 00 1 0 1 0 1 0 11 0 1 0 1 0 1 00 1 0 1 0 1 0 1The statement N = repmatNaN,2 3 creates a 2-by- 3 matrix of NaNs. Example 2 If you have code that uses repmat and also a binary operator or function, you can transform the code to use the bsxfun function inste
44、ad. In certain 第 11 页,共 88 页cases, this can provide a simpler and faster solution. This example replaces the sumof two repmat operations with a single call to bsxfun : x = 1:5; y = 1:10; % Replace this code repmatx,10,1 + repmaty,1,5 % with the following: bsxfunplus, x, y find Find indices and value
45、s of nonzero elementsSyntax ind = findXind = findX, k ind = findX, k, first ind = findX, k, last row,col = findX, . row,col,v = findX, . Description ind = findX locates all nonzero elementsindices of those elements in vector indof array X, and returns the linear . If X is a row vector, then ind is a
46、 row vector; otherwise, ind is a column vector. If X contains no nonzero elements or is an empty array, then ind is an empty array. ind = findX, k or ind = findX, k, first returns at most the first k indices corresponding to the nonzero entries of X. k must be a positive integer, but it can be of an
47、y numeric data type. ind = findX, k, last returns at most the last k indices corresponding col to the nonzero entries of X. row,col = findX, . returns the row and column indices of the nonzero entries in the matrix X. This syntax is especially useful when working with sparse matrices. If X is an N-d
48、imensional array with N 2, contains linear indices for the columns. For example, for a 5-by-7-by-3 array X with a nonzero element at X4,2,3 , find returns 4 in row and 16 in col . That is, 7 columns in page 1 + 7 columns in page 2 + 2 columns in page 3 = 16. row,col,v = findX, . returns a column or
49、row vector v of the nonzero entries in X, as well as row and column indices. If X is a logical expression, then v is a logical array. Output v contains the non-zero elements of the logical array obtained by evaluating the expression X. For example,A= magic4A = 16 2313 第 12 页,共 88 页511 10 897612 414
50、15 1r,c,v= findA10; r, c, v ans = 124413ans = 122344v is a logical array that contains the nonzero ans = 111111Here the returned vector elements of N where N=A10 Examples Example 1 X = 1 0 4 -3 0 0 0 8 6; indices = findX returns linear indices for the nonzero entries of indices = X. 13489Example 2 Y
51、ou can use a logical expression to define findX 2 X. For example, returns linear indices corresponding to the entries of X that are greater than 2. ans = 389Example 3 The following find command X = 3 2 0; -5 0 7; 0 0 1; r,c,v = findX returns a vector of row indices of the nonzero entries of X r = 1
52、2 12 3 a vector of column indices of the nonzero entries of X c = 1第 13 页,共 88 页1 23 3and a vector containing the nonzero entries of v = X. 3-5 27 1 Example 4 The expression X = 3 2 0; -5 0 7; 0 0 1; r,c,v = findX2 returns a vector of row indices of the nonzero entries of N where N=X2 r = 1 2a vecto
53、r of column indices of the nonzero entries of c = 1 3N where N=X2 and a logical v = array that contains the nonzero elements of N where N=X2 . 1 1Recall that when you use find on a logical expression, the output vector v does not contain the nonzero entries of the input array. Instead, it contains t
54、he nonzero values returned after evaluating the logical expression. Example 5 Some operations on a vector x = 11 0 33 0 55; findx ans = 135findx = 0 ans = 2第 14 页,共 88 页4find0 x & x 3, 4 returns the indices of the first four entries of 3. ans = 1 3 5 6 Example 7 Mthat are greater than If X is a vect
55、or of all zeros, findX returns an empty matrix. For example, indices = find0;0;0 indices = Empty matrix: 0-by-1 cell Create cell array Syntax C = celldim C = celldim1,.,dimN D = cellobj Description C = celldim creates a cell array of empty matrices. If dim is a scalar, C is dim-by-dim. If dim is a v
56、ector, C is dim1-by-.-dimN, where N is the number of elements of dim. C= celldim1,.,dimN creates cell array C, where Cis dim1-by-.-dimN. or System.Object into a MATLAB cell array. Tips Creating an empty array with the cell function, such as C = cell3,4,2; is exactly equivalent to assigning an empty
57、array to the last index of a new cell array: 第 15 页,共 88 页C3,4,2 = ; Input Arguments dim Scalar integer or vector of integers that specifies the dimensions of cell array C. dim1,.,dimN obj Scalar integers that specify the dimensions of C. One of the following: Java array or object .NET array of type
58、 System.String or Output Arguments C Cell array. Each cell contains an empty, 0-by-0 array of type double. D Cell array. Each cell contains a MATLAB type closest to the Java or .NET type. For more information, see: Conversion of Java Return Types .NET Type to MATLAB Type Mapping Examples Create an e
59、mpty 3-by-4-by-2 cell array. mycell = cell3,4,2; Create a cell array that is the samesize as mycell, created in the previous example. similar = cellsizemycell; Convert an array of java.lang.String objects into a MATLAB cell array. strArray = java_arrayjava.lang.String, 3; strArray1 = java.lang.Strin
60、gone; strArray2 = java.lang.Stringtwo; strArray3 = java.lang.Stringthree; cellArray = cellstrArray This code returns cellArray = one two three Create a cell array of folders in the c:work folder, using the .NET Framework System.IO.Directory class : myList = cellSystem.IO.Directory.GetDirectoriesc:wo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 个性化仓储解决方案服务外包协议范本版A版
- 2025年度企业员工心理健康培训服务合同范本8篇
- 中美洲2024年原材料供应协议
- 养殖场动物疫病防治服务合同(2025年度)3篇
- 2025版围栏施工安全风险评估与应急预案合同3篇
- 2025年度买卖合同担保条款修订版及风险防范策略
- 二零二五年度供暖设施安全检测与暖气安装合同样本
- 二零二五年度共有产权住房定金监管及分配合同4篇
- 2025年旋挖桩施工现场垃圾处理与环保合同3篇
- 二零二五年度厨具设备环保材料采购合同4篇
- 智慧校园信息化建设项目组织人员安排方案
- 浙教版七年级上册数学第4章代数式单元测试卷(含答案)
- 一病一品成果护理汇报
- AQ-T 1009-2021矿山救护队标准化考核规范
- 盐酸埃克替尼临床疗效、不良反应与药代动力学的相关性分析的开题报告
- 消防设施安全检查表
- 组合结构设计原理 第2版 课件 第6、7章 钢-混凝土组合梁、钢-混凝土组合剪力墙
- 建筑公司资质常识培训课件
- GB/T 26316-2023市场、民意和社会调查(包括洞察与数据分析)术语和服务要求
- 春节值班安全教育培训
- 带状疱疹护理查房
评论
0/150
提交评论