收藏 分销(赏)

Matlab知识总结.pdf

上传人:a199****6536 文档编号:2081859 上传时间:2024-05-15 格式:PDF 页数:92 大小:484.97KB
下载 相关 举报
Matlab知识总结.pdf_第1页
第1页 / 共92页
Matlab知识总结.pdf_第2页
第2页 / 共92页
Matlab知识总结.pdf_第3页
第3页 / 共92页
Matlab知识总结.pdf_第4页
第4页 / 共92页
Matlab知识总结.pdf_第5页
第5页 / 共92页
点击查看更多>>
资源描述

1、 1/92MatlabMatlab 笔记笔记基础操作基础操作1 1、基础命令、基础命令Cd+绝对路径 :改变当前目录Pwd :查看当前目录的绝对路径doc addpath :出现帮助菜单中关于 addpath 的知识help addpath :命令窗口出现 addpath 的介绍lookfor :关键词搜索命令,lookfor color 与 color 有关的函数 注:搜索时有时会出现大量信息,按 ctrl+C 可终止搜索,或者在使用该命令前先输入 more on 来启动分屏显示功能,输入 more off 回车,可以关闭分屏显示功能。What :查看当前目录下的文件那些和 matlab 有

2、关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

3、.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

4、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

5、 a blank file named fun.m in the specified folder,click Yes.2/92To 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 f

6、iles 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 c

7、lass.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 2、命令输入的几个实用方法、命令输入的几个实

8、用方法a、利用 tab 按键 输入命令的前几个字符,接着连按两下 tab 键,出现函数列表b、利用键盘上的上下方向键,调出历史命令c、当 matlab 按顺序执行很多命令时,可将要运行的大量命令统一存放在一个文本文件里,扩展名为.m,然后在命令窗口键入这个文件名,回车,matlab 就会一次性地执行这些命令3 3、matlabmatlab 保留常数保留常数Ans eps:当前计算机的零阈值i 和 f :若 i 和 f 值不被改写,则它们代表虚数单位,使用前,应确定它们没有被改写。恢复变量:i=sqrt(-1)。Inf :无穷大,负无穷用-inf 表示NaN :非数(not a number)N

9、argin :函数输入的变量的实际个数Nargout :函数输出变量的实际个数Pi :圆周率4 4、变量命名规则、变量命名规则 首个字符必须是字母,并且后面字符不能包含空格、标点和括号。Matlab 中大小写字符表示的含义不同5 5、数据类型结构、数据类型结构 利用 help datatype 查看所有的数据类型基本类型常用的:数值型:双精度、浮点数,其 matlab 表示为 double逻辑型:只包含 1,0。1 为 true,0 为 false字符型:赋值时,字符串是用单引号括起来的单元数据clear all%清除计算机内存所保存的变量和函数who%查看当前内存里的变量名whos%查看当前

10、内存里变量的详细说明 3/92clc%清屏注:命令行结尾加上一个分号表示不屏显结果,只运行命令。矩阵矩阵1 1、基本规则基本规则a、矩阵元素必须在中括号内b、同行元素之间用空格或逗号隔开c、行与行之间可以用分号或回车隔开d、如果矩阵的某行过长,可以在续行符后另起一行接着输入2 2、矩阵函数矩阵函数Ones :生成元素全为 1 的矩阵,ones(n)将生成 n*n 的矩阵Zeros :生成元素全为 0 的矩阵Nan :生成元素全为 nan 的矩阵Rand :生成在0,1区间均匀分布的随机矩阵Randn :生成均值为 0,方差为 1 的标注正态分布随机矩阵Eye :生成单位矩阵,主对角线元素全为

11、1Linspace :生成等间隔的行向量,其调用格式为:linspace(a,b,n),其中 a 和 b 是生成向量的第一个和最后一个元素,n 是元素总数Magic :魔方矩阵3 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

12、10 11s1,s2=size(a)%将矩阵 a 的每一维的长度赋值给 s1 和 s2 4/92 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

13、 逆时针旋转 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 文

14、件名变量名表-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 四舍五入

15、 a=1 2 3;4 5 6a(end,end)ans=6a(end-1,end)ans=2 5/92MatlabMatlab 指令及函数总结指令及函数总结logicallogical 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

16、 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.ndimsndimsNumber of array d

17、imensionsSyntaxn=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

18、(size(x).sizesizeArray dimensionsSyntaxd=size(X)m,n=size(X)6/92m=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=

19、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 equa

20、ls ndims(X).If n does not equal ndims(X),the following exceptions hold:n ndims(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 sing

21、le 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 2 7/92If 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 dimensi

22、ons 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 1lengthlengthLength of vector or largest array dimensionSyntaxnumberOfElements=length(array)DescriptionnumberOfElements=length(array)finds

23、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 z

24、ero.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=8 Create 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

25、(2,5,17,13);8/92length(Y)ans=17 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=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 1num

26、elNumber 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 variab

27、le 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 bra

28、ce 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 paragrap

29、h),numel is used to compute the number of 9/92expected 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

30、 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 ov

31、erloaded 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

32、 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=32reshapereshapeReshape 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=resh

33、ape(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.10/92B=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 th

34、e 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 dimensio

35、ns.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.

36、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 12permutepermuteRearrange 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

37、 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.Examples 11/92Given any matr

38、ix 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 12repmatrepmatReplicate and tile arraySyntaxB=repmat(A,m,n)B=repmat(A,m n)B=repmat(A,m n p.)Descr

39、iptionB=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 c

40、omposed 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

41、,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 copi

42、es of the second-order identity matrix,resulting in a checkerboard pattern.B=repmat(eye(2),3,4)B=12/92 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 re

43、pmat 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

44、)+repmat(y,1,5)%with the following:bsxfun(plus,x,y)findfindFind 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

45、 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 o

46、f 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 work

47、ing with sparse matrices.If X is an N-dimensional array 13/92with 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.

48、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

49、 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.

50、indices=1 3 4 8 9Example 2You can use a logical expression to define X.For example,find(X 2)returns linear indices corresponding to the entries of X that are greater than 2.ans=3 8 9Example 3The following find command 14/92X=3 2 0;-5 0 7;0 0 1;r,c,v=find(X)returns a vector of row indices of the nonz

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服