




已阅读5页,还剩87页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Matlab笔记基础操作1、基础命令Cd+绝对路径 :改变当前目录Pwd :查看当前目录的绝对路径doc addpath :出现帮助菜单中关于addpath的知识help addpath :命令窗口出现addpath的介绍lookfor :关键词搜索命令,lookfor color 与color有关的函数 注:搜索时有时会出现大量信息,按ctrl+C可终止搜索,或者在使用该命令前先输入more on来启动分屏显示功能,输入more off回车,可以关闭分屏显示功能。What :查看当前目录下的文件那些和matlab有关What general :列出matlab的基础函数名Which :可以很方便地给出某个函数的绝对路径Demo 回车 :可以进入matlab自带的演示教程home 回车 :将光标置于当前窗口的左上方(1)EditEdit or create fileAlternativesAs an alternative to the edit function, select File New or Open in the MATLAB desktop or any desktop tool.Syntaxeditedit fun.medit file.extedit fun1 fun2 fun3 .edit classname/funedit private/funedit classname/private/funedit +packagename/classname/funedit(my file.m)Descriptionedit opens a new editor window.edit fun.m opens the file fun.m in the default editor. The fun.m file specification 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 folder 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. To suppress the prompt, select Do not show this prompt again. To reinstate 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.edit 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 named class.edit +packagename/classname/fun opens a method for the named class in the named package.edit(my 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、利用键盘上的上下方向键,调出历史命令c、当matlab按顺序执行很多命令时,可将要运行的大量命令统一存放在一个文本文件里,扩展名为.m,然后在命令窗口键入这个文件名,回车,matlab就会一次性地执行这些命令3、matlab保留常数Ans eps:当前计算机的零阈值i和f :若i和f值不被改写,则它们代表虚数单位,使用前,应确定它们没有被改写。恢复变量:i=sqrt(-1)。Inf :无穷大,负无穷用-inf表示NaN :非数(not a number)Nargin :函数输入的变量的实际个数Nargout :函数输出变量的实际个数Pi :圆周率4、变量命名规则 首个字符必须是字母,并且后面字符不能包含空格、标点和括号。Matlab中大小写字符表示的含义不同5、数据类型结构 利用help datatype查看所有的数据类型基本类型常用的:数值型:双精度、浮点数,其matlab表示为double逻辑型:只包含1,0。1为true,0为false字符型:赋值时,字符串是用单引号括起来的单元数据clear all %清除计算机内存所保存的变量和函数who %查看当前内存里的变量名whos %查看当前内存里变量的详细说明clc %清屏注:命令行结尾加上一个分号表示不屏显结果,只运行命令。矩阵1、 基本规则a、 矩阵元素必须在中括号内b、 同行元素之间用空格或逗号隔开c、 行与行之间可以用分号或回车隔开d、 如果矩阵的某行过长,可以在续行符后另起一行接着输入2、 矩阵函数Ones :生成元素全为1的矩阵,ones(n)将生成n*n的矩阵Zeros :生成元素全为0的矩阵Nan :生成元素全为nan的矩阵Rand :生成在0,1区间均匀分布的随机矩阵Randn :生成均值为0,方差为1的标注正态分布随机矩阵Eye :生成单位矩阵,主对角线元素全为1Linspace :生成等间隔的行向量,其调用格式为:linspace(a,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 11s1,s2=size(a) %将矩阵a的每一维的长度赋值给s1和s2 S1=4 s2=4b1=length(b) %向量b的长度 b=-3 -2 -1 0 1 2 b1= 6ndims(a) %直接得到a的维数 Ans=2f、空矩阵的应用空矩阵的表示a(3,:)= %删除矩阵a的第3行h、矩阵的翻转(1)利用撇号可以实现矩阵的行列互换(2)fliplr(a) %将矩阵a左右翻转,第一列变成最后一列,以此类推 flipud(a) %将矩阵a上下翻转,第一行变成最后一行,以此类推 rot90(a) %将矩阵a逆时针旋转90度Rot90(a,k) :将矩阵a逆时针旋转k倍的90度,k为1可省略g、矩阵的拼接 b=a ones(3,6)*2;nan(size(a) a-1k、基本函数操作 help elfun和help matfun可以查看基本的数学函数和线性代数运算函数列表sum(a) %对矩阵a的每一列求和sum(a(:); %对矩阵a的全部元素求和 a(:)将矩阵a变成向量a补充:type +文件名 %查看当期文件目录下的内容 利用mat文件可以将当前matlab工作空间中的一些有用的变量长久地保留下来,扩展名为.mat,mat文件生成和装入用save和load命令完成 常用格式: Save 文件名变量名表-append-ascii load 文件名变量名表-asciisave wps %保存为wps.mat文件ls wps.matload wps %装入mat文件save wps x y %只保存x y变量load wps x % 只装入x变量help tic/toc %时间机器 取整函数: fix floor ceil roundfix(1.2)=1 fix(2.3)=2 靠近0取整floor(1.2)=1 floor(-1.2)=2 靠近负无穷取整ceil(1.2)=2 ceil(-1.2)=-1 靠近正无穷取整round(1.2)=1 round(1.6)=2 四舍五入 a= 1 2 3;4 5 6a(end,end)ans=6a(end-1,end) ans=2Matlab指令及函数总结logical Convert numeric values to logicalSyntax K = logical(A)K = logical(A) returns an array that can be used for logical indexing or logical tests.ExamplesGiven A = 1 2 3; 4 5 6; 7 8 9, the statement B = logical(eye(3) returns a logical arrayB = 1 0 0 0 1 0 0 0 1which can be used in logical indexing that returns As diagonal elements:A(B)ans = 1 5 9However, attempting to index into A using the numeric array eye(3) results in:A(eye(3)? Subscript indices must either be real positive integers or logicals.ndimsNumber of array dimensionsSyntaxn = ndims(A)Descriptionn = ndims(A) 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 size(A,dim) = 1.Algorithmsndims(x) is length(size(x).sizeArray dimensionsSyntaxd = size(X)m,n = size(X)m = size(X,dim)d1,d2,d3,.,dn = size(X),Descriptiond = size(X) returns the sizes of each dimension of array X in a vector d with ndims(X) elements. If X is a scalar, which MATLAB software regards as a 1-by-1 array, size(X) returns the vector 1 1.m,n = size(X) returns the size of matrix X in separate variables m and n.m = size(X,dim) returns the size of the dimension of X specified by scalar dim.d1,d2,d3,.,dn = size(X), for n 1, returns the sizes of the dimensions of the array X in the variables d1,d2,d3,.,dn, provided the number of output arguments n equals ndims(X). If n does not equal ndims(X), the following exceptions hold:nndims(X)size returns ones in the extra variables, that is, those corresponding to ndims(X)+1 through n.ExamplesExample 1The size of the second dimension of rand(2,3,4) is 3.m = size(rand(2,3,4),2)m = 3Here the size is output as a single vector.d = size(rand(2,3,4)d = 2 3 4Here the size of each dimension is assigned to a separate variable.m,n,p = size(rand(2,3,4)m = 2n = 3p = 4Example 2If X = ones(3,4,5), thend1,d2,d3 = size(X)d1 = d2 = d3 = 3 4 5But when the number of output variables is less than ndims(X):d1,d2 = size(X)d1 = d2 = 3 20The extra dimensions are collapsed into a single product. If n ndims(X), the extra variables all represent singleton dimensions:d1,d2,d3,d4,d5,d6 = size(X)d1 = d2 = d3 = 3 4 5d4 = d5 = d6 = 1 1 1lengthLength of vector or largest array dimensionSyntaxnumberOfElements = length(array)DescriptionnumberOfElements = length(array) 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, numberOfElements is equivalent to max(size(array). For empty arrays, numberOfElements is zero.ExamplesCreate a 1-by-8 array X and use length to find the number of elements in the second (largest) dimension:X = 5, 3.4, 72, 28/4, 3.61, 17 94 89;length(X)ans = 8Create a 4-dimensional array Y in which the third dimension is the largest. Use length to find the number of elements in that dimension:Y = rand(2, 5, 17, 13);length(Y)ans = 17Create 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 = struct(f1, Name:, f2, Charlie, . f3, DOB:, f4, 1917)S = f1: Name: f2: Charlie f3: DOB: f4: 1917structfun(field)length(field), S)ans = 5 7 4 1numelNumber of elements in array or subscripted array expressionSyntaxn = numel(A)n = numel(A, index1, index2, . indexn)Descriptionn = numel(A) returns the number of elements, n, in array A.n = numel(A, index1, index2, . indexn) returns the number of subscripted elements, n, in A(index1, index2, ., indexn). To handle the variable number of arguments, numel is typically written with the header function n = numel(A, varargin), where varargin is a cell array with elements index1, index2, . indexn.The MATLAB software 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).TipsIt is important to note the significance of numel with regards to the overloaded 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 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 returned by the 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 consistent with the class subsref and subsasgn functions. Otherwise, MATLAB produces errors when calling these functions.ExamplesCreate a 4-by-4-by-2 matrix. numel counts 32 elements in the matrix.a = magic(4);a(:,:,2) = aa(:,:,1) = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1a(:,:,2) = 16 5 9 4 2 11 7 14 3 10 6 15 13 8 12 1numel(a)ans = 32reshapeReshape arraySyntaxB = reshape(A,m,n)B = reshape(A,m,n,p,.)B = reshape(A,m n p .)B = reshape(A,.,.)B = reshape(A,siz)DescriptionB = reshape(A,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(A,m,n,p,.) or B = reshape(A,m n p .) returns an n-dimensional array with the same elements as A 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 prod(size(A).B = reshape(A,.,.) calculates the length of the dimension represented by the placeholder , such that the product of the dimensions equals prod(size(A). The value of prod(size(A) must be evenly divisible by the product of the specified dimensions. You can use only one occurrence of .B = reshape(A,siz) returns an n-dimensional array with the same elements as A, but reshaped to siz, a vector representing the dimensions of the reshaped array. The quantity prod(siz) must be the same as prod(size(A).ExamplesReshape a 3-by-4 matrix into a 2-by-6 matrix.A = 1 4 7 10 2 5 8 11 3 6 9 12 B = reshape(A,2,6) B = 1 3 5 7 9 11 2 4 6 8 10 12B = reshape(A,2,) B = 1 3 5 7 9 11 2 4 6 8 10 12permuteRearrange dimensions of N-D arraySyntaxB = permute(A,order)DescriptionB = permute(A,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 specified by order. All the elements of order must be unique.Tipspermute and ipermute are a generalization of transpose (.) for multidimensional arrays.ExamplesGiven any matrix A, the statementpermute(A,2 1) is the same as A.For example:A = 1 2; 3 4; permute(A,2 1)ans = 1 3 2 4The following code permutes a three-dimensional array:X = rand(12,13,14);Y = permute(X,2 3 1);size(Y)ans = 13 14 12repmatReplicate and tile arraySyntaxB = repmat(A,m,n)B = repmat(A,m n)B = repmat(A,m n p.)DescriptionB = repmat(A,m,n) creates a large matrix B consisting of an m-by-n tiling of copies of A. The size of B is size(A,1)*m, (size(A,2)*n. The statement repmat(A,n) creates an n-by-n tiling.B = repmat(A,m n) accomplishes the same result as repmat(A,m,n).B = repmat(A,m n p.) produces a multidimensional array B composed of copies of A. The size of B is size(A,1)*m, size(A,2)*n, size(A,3)*p, .Tipsrepmat(A,m,n), 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:repmat(NaN,m,n) returns the same result as NaN(m,n).repmat(single(inf),m,n) is the same as inf(m,n,single).repmat(int8(0),m,n) is the same as zeros(m,n,int8).repmat(uint32(1),m,n) is the same as ones(m,n,uint32).repmat(eps,m,n) is the same as eps(ones(m,n).ExamplesExample 1In this example, repmat replicates 12 copies of the second-order identity matrix, resulting in a checkerboard pattern.B = repmat(eye(2),3,4)B = 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 The statement N = repmat(NaN,2 3) creates a 2-by-3 matrix of NaNs.Example 2If you have code that uses repmat and also a binary operator or function, you can transform the code to use the bsxfun function instead. In certain cases, this can provide a simpler and faster solution.This example replaces the sum of two repmat operations with a single call to bsxfun:x = 1:5; y = (1:10); % Replace this coderepmat(x,10,1) + repmat(y,1,5) % with the following:bsxfun(plus, x, y)findFind indices and values of nonzero elementsSyntaxind = find(X)ind = find(X, k)ind = find(X, k, first)ind = find(X, k, last)row,col = find(X, .)row,col,v = find(X, .)Descriptionind = find(X) locates all nonzero elements of array X, and returns the linear indices of those elements in vector ind. If X is a row vector, then ind is a 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 = find(X, k) or ind = find(X, 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 any numeric data type.ind = find(X, k, last) returns at most the last k indices corresponding to the nonzero entries of X.row,col = find(X, .) 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-dimensional array with N 2, col contains linear indices for the columns. For example, for a 5-by-7-by-3 array X with a nonzero element at X(4,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 = find(X, .) returns a column or 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= magic(4)A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1r,c,v= find(A10);r, c, vans = 1 2 4 4 1 3ans = 1 2 2 3 4 4ans = 1 1 1 1 1 1Here the returned vector v is a logical array that contains the nonzero elements of N whereN=(A10)ExamplesExample 1X = 1 0 4 -3 0 0 0 8 6;indices = find(X)returns linear indices for the nonzero entries of X.indices = 1 3 4 8 9Example 2You can use a logical expression to define X. For example,find(X 2)r
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 豫剧花木兰课件
- 2025年度数据中心内部设备安装合同协议
- 2025版汽车维修厂维修车间维修技师劳动合同范本
- 2025年度个人信用贷款担保及审核合同
- 2025版跨国企业外教引进与海外员工语言提升服务合同
- 2025年车辆抵押借款合同关键条款分析
- 2025代持股权转让与公司战略调整合作协议
- 2025大型设备运输合同范本
- 2025年版云南省劳动合同范本下载
- 红绿灯课件教学课件
- 加油、加气、充电综合站项目可行性研究报告
- 2025保密协议范本:物流行业货物信息保密
- 塔机拆卸合同范本
- 2024-2025学年广东省深圳市南山区四年级(下)期末数学试卷
- 《煤矿安全规程(2025版)》知识培训
- 2025秋数学(新)人教五年级(上)第1课时 小数乘整数
- 半导体行业面试问题及答案解析
- 《数字技术应用基础模块》技工中职全套教学课件
- 房屋拆除专项施工方案(3篇)
- AutoCAD电气工程制图 课件 项目1 低压配电柜的绘制与识图
- 2025至2030年中国绿色船舶行业发展前景预测及投资方向研究报告
评论
0/150
提交评论