Matlab知识总结Word格式文档下载.docx
《Matlab知识总结Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Matlab知识总结Word格式文档下载.docx(102页珍藏版)》请在冰豆网上搜索。
editfun.m
editfile.ext
editfun1fun2fun3...
editclassname/fun
editprivate/fun
editclassname/private/fun
edit+packagename/classname/fun
edit('
myfile.m'
)
Description
editopensaneweditorwindow.
editfun.mopensthefilefun.minthedefaulteditor.Thefun.mfilespecificationcanincludeapartialpath,completepath,relativepath,ornopath.Beawareofthefollowing:
Ifyoudonotspecifyapath,thecurrentfolderisthedefault.
Ifyouspecifyapath,thefoldermustexist;
otherwiseMATLABreturnsanerror.
Ifyouspecifyapathandthefolderexits,butthespecifiedfiledoesnot,apromptopenssuchasshowninthefollowingimage:
Tocreateablankfilenamedfun.minthespecifiedfolder,clickYes.Tosuppresstheprompt,selectDonotshowthispromptagain.Toreinstatethepromptaftersuppressingit,openthePreferencesdialogboxbyselectingFile>
Preferences>
General>
ConfirmationDialogsandthenselectingPromptwheneditingfilesthatdonotexistinthepaneontheright.
editfile.extopensthespecifiedfile.
editfun1fun2fun3...opensfun1.m,fun2.m,fun3.m,andsoon,inthedefaulteditor.
editclassname/fun,oreditprivate/fun,oreditclassname/private/funopensamethod,privatefunction,orprivatemethodforthenamedclass.
edit+packagename/classname/funopensamethodforthenamedclassinthenamedpackage.
)opensthefilemyfile.minthedefaulteditor.Thisformoftheeditfunctionisusefulwhenafilenamecontainsaspace;
youcannotusethecommandforminsuchacase.
2、命令输入的几个实用方法
a、利用tab按键
输入命令的前几个字符,接着连按两下tab键,出现函数列表
b、利用键盘上的上下方向键,调出历史命令
c、当matlab按顺序执行很多命令时,可将要运行的大量命令统一存放在一个文本文件里,扩展名为.m,然后在命令窗口键入这个文件名,回车,matlab就会一次性地执行这些命令
3、matlab保留常数
Anseps:
当前计算机的零阈值
i和f:
若i和f值不被改写,则它们代表虚数单位,使用前,应确定它们没有被改写。
恢复变量:
i=sqrt(-1)。
Inf:
无穷大,负无穷用-inf表示
NaN:
非数(notanumber)
Nargin:
函数输入的变量的实际个数
Nargout:
函数输出变量的实际个数
Pi:
圆周率
4、变量命名规则
首个字符必须是字母,并且后面字符不能包含空格、标点和括号。
Matlab中大小写字符表示的含义不同
5、数据类型结构
利用helpdatatype查看所有的数据类型基本类型
常用的:
数值型:
双精度、浮点数,其matlab表示为double
逻辑型:
只包含1,0。
1为true,0为false
字符型:
赋值时,字符串是用单引号括起来的
单元数据
>
clearall%清除计算机内存所保存的变量和函数
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:
生成单位矩阵,主对角线元素全为1
Linspace:
生成等间隔的行向量,其调用格式为:
linspace(a,b,n),其中a和b是生成向量的第一个和最后一个元素,n是元素总数
Magic:
魔方矩阵
3、矩阵运算
+-*/(右除)\(左除)^(乘方)‘(转置)
a、点运算
点运算符号有.*./.\.^
两矩阵进行点运算是指对它们的对应元素进行相关运算,要求两个矩阵的维数相同
b、关系运算
<
>
=>
===(等于)~=(不等于)
运算规则
(1)当对象为两个标量,则直接比较大小关系,成立结果为1,否则为0
(2)当参与比较的是两个维数相同的矩阵时,比较是对两个矩阵相同位置的元素按标量关系运算规则逐个进行,结果为一个维数与原矩阵相同的矩阵,元素由0或1组成
(3)当一个是标量,一个是矩阵时,则把标量与矩阵的每一个元素按标量关系运算规则逐个比较,并给出一个维数与原矩阵相同的矩阵,由0或1组成
C、逻辑运算
&
(与)|(或)~(非)
d、冒号操作
b=-3:
2%生成间隔为1的向量
c=130:
-2.4:
115%中间可以指定任意间隔
d=a(:
3)%提取矩阵a的第3列元素,并转置后赋值给变量d
E、矩阵的维数
a=
1234
78910
3469
571011
[s1,s2]=size(a)%将矩阵a的每一维的长度赋值给s1和s2
S1=4s2=4
b1=length(b)%向量b的长度b=-3-2-1012
b1=6
ndims(a)%直接得到a的维数
Ans=2
f、空矩阵的应用
空矩阵的表示[]
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=[aones(3,6)*2;
nan(size(a))a-1]
k、基本函数操作
helpelfun和helpmatfun可以查看基本的数学函数和线性代数运算函数列表
sum(a)%对矩阵a的每一列求和
sum(a(:
));
%对矩阵a的全部元素求和a(:
)将矩阵a变成向量a
补充:
type+文件名%查看当期文件目录下的内容
利用mat文件可以将当前matlab工作空间中的一些有用的变量长久地保留下来,扩展名为.mat,mat文件生成和装入用save和load命令完成
常用格式:
Save文件名[变量名表][-append][-ascii]
load文件名[变量名表][-ascii]
savewps%保存为wps.mat文件
ls
wps.mat
loadwps%装入mat文件
savewpsxy%只保存xy变量
loadwpsx%只装入x变量
helptic/toc%时间机器
取整函数:
fixfloorceilround
fix(1.2)=1fix(2.3)=2靠近0取整
floor(1.2)=1floor(-1.2)=2靠近负无穷取整
ceil(1.2)=2ceil(-1.2)=-1靠近正无穷取整
round(1.2)=1round(1.6)=2四舍五入
a=[123;
456]
a(end,end)
ans=6
a(end-1,end)
ans=2
Matlab指令及函数总结
☆logical
Convertnumericvaluestological
K=logical(A)
K=logical(A)returnsanarraythatcanbeusedforlogicalindexingorlogicaltests.
Examples
GivenA=[123;
456;
789],thestatementB=logical(eye(3))returnsalogicalarray
B=
100
010
001
whichcanbeusedinlogicalindexingthatreturnsA'
sdiagonalelements:
A(B)
ans=
1
5
9
However,attemptingtoindexintoAusingthenumericarrayeye(3)resultsin:
A(eye(3))
?
Subscriptindicesmusteitherberealpositiveintegersor
logicals.
☆ndims
Numberofarraydimensions
n=ndims(A)
n=ndims(A)returnsthenumberofdimensionsinthearrayA.Thenumberofdimensionsinanarrayisalwaysgreaterthanorequalto2.Trailingsingletondimensionsareignored.Asingletondimensionisanydimensionforwhichsize(A,dim)=1.
Algorithms
ndims(x)islength(size(x)).
☆size
Arraydimensions
d=size(X)
[m,n]=size(X)
m=size(X,dim)
[d1,d2,d3,...,dn]=size(X),
d=size(X)returnsthesizesofeachdimensionofarrayXinavectordwithndims(X)elements.IfXisascalar,whichMATLABsoftwareregardsasa1-by-1array,size(X)returnsthevector[11].
[m,n]=size(X)returnsthesizeofmatrixXinseparatevariablesmandn.
m=size(X,dim)returnsthesizeofthedimensionofXspecifiedbyscalardim.
[d1,d2,d3,...,dn]=size(X),forn>
1,returnsthesizesofthedimensionsofthearrayXinthevariablesd1,d2,d3,...,dn,providedthenumberofoutputargumentsnequalsndims(X).Ifndoesnotequalndims(X),thefollowingexceptionshold:
n
<
ndims(X)
diequalsthesizeoftheithdimensionofXfor,butdnequalstheproductofthesizesoftheremainingdimensionsofX,thatis,dimensionsnthroughndims(X).
sizereturnsonesinthe"
extra"
variables,thatis,thosecorrespondingtondims(X)+1throughn.
Example1
Thesizeoftheseconddimensionofrand(2,3,4)is3.
m=size(rand(2,3,4),2)
m=
3
Herethesizeisoutputasasinglevector.
d=size(rand(2,3,4))
d=
234
Herethesizeofeachdimensionisassignedtoaseparatevariable.
[m,n,p]=size(rand(2,3,4))
2
n=
p=
4
Example2
IfX=ones(3,4,5),then
[d1,d2,d3]=size(X)
d1=d2=d3=
345
Butwhenthenumberofoutputvariablesislessthanndims(X):
[d1,d2]=size(X)
d1=d2=
320
The"
dimensionsarecollapsedintoasingleproduct.
Ifn>
ndims(X),the"
variablesallrepresentsingletondimensions:
[d1,d2,d3,d4,d5,d6]=size(X)
d4=d5=d6=
111
☆length
Lengthofvectororlargestarraydimension
numberOfElements=length(array)
numberOfElements=length(array)findsthenumberofelementsalongthelargestdimensionofanarray.arrayisanarrayofanyMATLABdatatypeandanyvaliddimensions.numberOfElementsisawholenumberoftheMATLABdoubleclass.
Fornonemptyarrays,numberOfElementsisequivalenttomax(size(array)).Foremptyarrays,numberOfElementsiszero.
Createa1-by-8arrayXanduselengthtofindthenumberofelementsinthesecond(largest)dimension:
X=[5,3.4,72,28/4,3.61,179489];
length(X)
8
Createa4-dimensionalarrayYinwhichthethirddimensionisthelargest.Uselengthtofindthenumberofelementsinthatdimension:
Y=rand(2,5,17,13);
length(Y)
17
CreateastructarraySwithcharacterandnumericfieldsofdifferentlengths.UsethestructfunfunctiontoapplylengthtoeachfieldofS:
S=struct('
f1'
'
Name:
'
f2'
Charlie'
...
'
f3'
DOB:
f4'
1917)
S=
f1:
f2:
f3:
f4:
1917
structfun(@(field)length(field),S)
7
☆numel
Numberofelementsinarrayorsubscriptedarrayexpression
n=numel(A)
n=numel(A,index1,index2,...indexn)
n=numel(A)returnsthenumberofelements,n,inarrayA.
n=numel(A,index1,index2,...indexn)returnsthenumberofsubscriptedelements,n,inA(index1,index2,...,indexn).Tohandlethevariablenumberofarguments,numelistypicallywrittenwiththeheaderfunctionn=numel(A,varargin),wherevararginisacellarraywithelementsindex1,index2,...indexn.
TheMATLABsoftwareimplicitlycallsthenumelbuilt-infunctionwheneveranexpressiongeneratesacomma-separatedlist.Thisincludesbraceindexing(i.e.,A{index1,index2,...,indexN}),anddotindexing(i.e.,A.fieldname).
Tips
Itisimportanttonotethesignificanceofnumelwithregardstotheoverloadedsubsrefandsubsasgnfunctions.Inthecaseoftheoverloadedsubsreffunctionforbraceanddotindexing(asdescribedinthelastparagraph),numelisusedtocomputethenumberofexpectedoutputs(nargout)returnedfromsubsref.Fortheoverloadedsubsasgnfunction,numelisusedtocomputethenumberofexpectedinputs(nargin)tobeassignedusingsubsasgn.Thenarginvaluefortheoverloadedsubsasgnfunctionisthevaluereturnedbynumelplus2(oneforthevariablebeingassignedto,andoneforthestructurearrayofsubscripts).
Asaclassdesigner,yo