matlab常用语言汇总2.docx
《matlab常用语言汇总2.docx》由会员分享,可在线阅读,更多相关《matlab常用语言汇总2.docx(14页珍藏版)》请在冰豆网上搜索。
matlab常用语言汇总2
buptmatlab@
buptmatlabyxl
>>a=[123]
a=
123
>>a=[1,2,3]
a=
123
>>a=[1,2,3]'
a=
1
2
3
>>a=[1,2,3;4,5,6]
a=
123
456
>>
>>t=0:
0.01:
2*pi
>>signal=sin(t);
>>plot(t,signal)
>>signal=sin(10t);
signal=sin(10t);
|
错误:
不应为MATLAB表达式。
>>signal=sin(10*t);
>>plot(t,signal)
>>signal=sin(t);
plot(t,signal)
>>plot(t,signal)
>>holdon
>>sii=sin(10*t);
>>plot(t,sii,'r')
>>helpplot
plot-2-Dlineplot
ThisMATLABfunctioncreatesa2-DlineplotofthedatainYversusthe
correspondingvaluesinX.IfXandYarebothvectors,thentheymusthaveequal
lengthandMATLABplotsYversusX.IfXandYarebothmatrices,thentheymust
haveequalsizeandMATLABplotscolumnsofYversuscolumnsofX.IfoneofXor
Yisavectorandtheotherisamatrix,thenthematrixmusthavedimensions
suchthatoneofitsdimensionsequalsthevectorlength.
plot(X,Y)
plot(X,Y,LineSpec)
plot(X1,Y1,...,Xn,Yn)
plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn)
plot(Y)
plot(Y,LineSpec)
plot(___,Name,Value)
plot(axes_handle,___)
h=plot(___)
plot的参考页
另请参阅gca,hold,legend,LineSpec,title,xlabel,xlim,ylabel,ylim
名为plot的其他函数
simulink/plot,curvefit/plot,finance/plot,fixedpoint/plot,mpc/plot,
rf/plot,wavelet/plot,simscape/simscape.logging.plot
>>plot(sii,signall,t)
未定义函数或变量'signall'。
是不是想输入:
>>plot(sii,signal,t)
错误使用plot
数据必须为单一矩阵Y或X,Y配对列表
>>plot(sii,t,signal,t)
>>plot(t,sii,t,signal)
>>h=plot(t,signal)
h=
262.0011
>>set(h,'color','r')
>>plot(t,signal)
>>plot(t,signal)
>>plot(t,signal)
>>set(h,'color','r')
错误使用handle.handle/set
对象无效或已删除。
>>h=plot(t,signal)
h=
174.0048
>>set(h,'color','r')
>>set(h,'linewidth',5)
>>a=[5,1,2,3]
a=
5123
>>a
(1)
ans=
5
>>a(3)
ans=
2
>>b=[1,2,3;4,5,6]
b=
123
456
>>b(2,3)
ans=
6
>>b(3,2)
索引超出矩阵维度。
>>b(3,2)=7
b=
123
456
070
>>b(100,101)
索引超出矩阵维度。
>>b(100,101)=5
>>a=[1,2,3,4,5,6,7,8,9,10;11,12,13,14,15,16,17,18,19,20]
a=
12345678910
11121314151617181920
>>a=[1:
10;11:
20]
a=
12345678910
11121314151617181920
>>
>>c=3+4i
c=
3.0000+4.0000i
>>c1=real(c)
c1=
3
>>c2=imag(c)
c2=
4
>>c3=abs(c)
c3=
5
>>aa=c3^2-c2^2-c1^
aa=c3^2-c2^2-c1^
|
错误:
表达式或语句不完整或不正确。
>>aa=c3^2-c2^2-c1^2
aa=
0
>>formatlong
>>aa
aa=
0
>>theta=atan(c2/c3)
theta=
0.674740942223553
>>theta=atan(c2/c1)
theta=
0.927295218001612
>>formatshort
>>cc=5*exp(i*theta)
cc=
3.0000+4.0000i
>>close
>>
>>a=[123;456;789]
a=
123
456
789
>>length(a)
ans=
3
>>size(a)
ans=
33
>>nr=length(a)
nr=
3
>>nc=col(a)
未定义与'double'类型的输入参数相对应的函数'col'。
>>helplength
length-Lengthofvectororlargestarraydimension
ThisMATLABfunctionfindsthenumberofelementsalongthelargestdimensionof
anarray.
numberOfElements=length(array)
length的参考页
另请参阅ndims,numel,size
名为length的其他函数
finance/length,fixedpoint/length,instrument/length,distcomp/length,
matlab/length(serial)
>>nl=size(a)
nl=
33
>>[nc,nl]=size(a)
nc=
3
nl=
3
>>ones(nc,nl)
ans=
111
111
111
>>f1=[123];
>>f1=[123]
f1=
123
>>fliplr(f1)
ans=
321
>>flipud(f1)
ans=
123
>>zeros(5)
ans=
00000
00000
00000
00000
00000
>>f2[f1f1]
f2[f1f1]
|
错误:
圆括号或方括号不对称或异常。
>>f2=[f1f1]
f2=
123123
>>magic(3)
ans=
816
357
492
>>magic(12)
ans=
14423141140671371361011133
131311301617127126202112312224
251191182829115114323311111036
10838391051044243101100464797
965051939254558988585985
618382646579786869757472
737170767767668081636284
608687575690915352949549
4898994544102103414010610737
109353411211331301161172726120
121232212412519181281291514132
1213413598138139541421431
>>d=magic(3)
d=
816
357
492
>>d(1,)
d(1,)
|
错误:
圆括号或方括号不对称或异常。
>>d
(1)
ans=
8
>>d(1,)
d(1,)
|
错误:
圆括号或方括号不对称或异常。
>>d(1,:
)
ans=
816
>>sum(d(1,:
)
sum(d(1,:
)
|
错误:
表达式或语句不正确--可能(、{或[不对称。
>>sum(d(1,:
))
ans=
15
>>sum(d(:
2))
ans=
15
>>sum(d)
ans=
151515
>>a=[123;456;789]
a=
123
456
789
>>sum(a)
ans=
121518
>>%按列优先
>>sum(a')
ans=
61524
>>%‘转置
>>rand(1,10)
ans=
0.81470.90580.12700.91340.63240.09750.27850.54690.95750.9649
>>a=rand(1,10000);
>>sum(a)/10000
ans=
0.4994
>>sum(a)/length(a)
ans=
0.4994
>>helpsum
sum-Sumofarrayelements
ThisMATLABfunctionreturnsthesumoftheelementsofAalongthefirstarray
dimensionwhosesizedoesnotequal1:
S=sum(A)
S=sum(A,dim)
S=sum(___,type)
sum的参考页
另请参阅cumsum,diff,isfloat,prod
名为sum的其他函数
fixedpoint/sum
>>prod(a)
ans=
0
>>b=randn(1,10)
b=
-1.0896-1.22080.7074-0.0699-0.4863-0.2566-0.1070-0.9489-0.2113-0.8603
>>mean(b)
ans=
-0.4543
>>var(b)
ans=
0.3464
>>b=randn(1,10000)
>>var(b)
ans=
0.9757
>>2*randn(1,10)
ans=
-3.61760.33041.51051.1306-0.84281.8199-2.6268-3.94682.02010.2183
>>helphist
hist-Histogramplot
ThisMATLABfunctioncreatesahistogrambarplotofdata.
hist(data)
hist(data,nbins)
hist(data,xvalues)
hist(axes_handle,___)
nelements=hist(___)
[nelements,centers]=hist(___)
hist的参考页
另请参阅bar,ColorSpec,histc,mode,patch,rose,stairs
名为hist的其他函数
finance/hist,fixedpoint/hist
>>hist(b)
>>hist(b,1000)
>>eye(4)
ans=
1000
0100
0010
0001
>>c=magic(3)
c=
816
357
492
>>eye(c)
错误使用eye
不支持N维数组。
>>sum
>var(b)
ans=
0.9757
>>2*randn(1,10)
ans=
-3.61760.33041.51051.1306-0.84281.8199-2.6268-3.94682.02010.2183
>>helphist
hist-Histogramplot
ThisMATLABfunctioncreatesahistogrambarplotofdata.
hist(data)
hist(data,nbins)
hist(data,xvalues)
hist(axes_handle,___)
nelements=hist(___)
[nelements,centers]=hist(___)
hist的参考页
另请参阅bar,ColorSpec,histc,mode,patch,rose,stairs
名为hist的其他函数
finance/hist,fixedpoint/hist
>>hist(b)
>>hist(b,1000)
>>eye(4)
ans=
1000
0100
0010
0001
>>c=magic(3)
c=
816
357
492
>>eye(c)
错误使用eye
不支持N维数组。
>>b=eye(3)
b=
100
010
001
>>c.*b
ans=
800
050
002
>>sum(sum(c.*b))
ans=
15
>>linspace(0,1);
>>linspace(0,1,10)
ans=
00.11110.22220.33330.44440.55560.66670.77780.88891.0000
>>a=logspace(0,2,10)
a=
1.00001.66812.78264.64167.742612.915521.544335.938159.9484100.0000
>>plot(a)
>>