admin 管理员组文章数量: 887006
【Matlab】基本操作笔记(已完结)
文章目录
- 引言
- 一,基本操作
- -1.1普通数据操作
- -1.2矩阵操作
- -1.3绘制图像
- -1.4帮助函数
- 二,移动数据
- -2.1文件操作
- -2.1.1文件基本操作
- -2.1.2文件读入
- -2.1.3文件导出
- -2.2矩阵拓展操作
- 三,计算数据
- 四,数据绘制
- 五,流程控制
- -5.1 if语句
- -5.2 for语句
- -5.3 while语句
- -5.4 break语句
- -5.5 函数
- 六,矢量
引言
无论是Octave还是Matlab,矩阵运算都是按照列优先
注:此处用的是
Matlab2016a
编码
一,基本操作
-1.1普通数据操作
>> 5+6ans =11>> 3-2ans =1>> 5*8ans =40>> 1/2ans =0.5000>> 2^6 %2的6次方ans =64>> 1==2 %判断是否相等ans =0>> 1~=2 %判断是否不等ans =1>> 1&&0 %逻辑与ans =0>> 1||0 %逻辑或ans =1>> xor(1,0) %逻辑异或ans =1>> PS1('>>') %在Octive中是把输出换成仅有>>的格式
未定义函数或变量 'PS1'。>> a=3a =3>> a=3; %如果加上;控制台就无法打印数据>> disp(a) %打印数据a3>> b='hi' b =hi>> c=(3>=1)c =1>> a=pi %pi就是圆周率a =3.1416>> disp(a) 3.1416>> disp(sprintf('2 decimals:%2f',a)) %旧版c语言输出
2 decimals:3.141593
>> aa =3.1416>> format long %更改数据格式long
>> aa =3.141592653589793>> format short %更改数据格式为short
>> aa =3.1416
>>clear %清除工作区数据
>>clc %清除命令行数据
-1.2矩阵操作
>> A=[1 2;3 4;5 6]A =1 23 45 6>> A=[1 2;
3 4;
5 6]A =1 23 45 6>> v=[1 2 3]v =1 2 3>> v=[1;2;3]v =123>> v=1:0.1:2 %从1到2,步伐为0.1v =1 至 8 列1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.70009 至 11 列1.8000 1.9000 2.0000>> v=1:6 %从1到6步伐为1v =1 2 3 4 5 6>> ones(2,3) %2行3列元素值都是1的矩阵ans =1 1 11 1 1>> c=2*ones(2,3)c =2 2 22 2 2>> c=[2 2 2;2 2 2]c =2 2 22 2 2>> w=ones(1,3)w =1 1 1>> w=zeros(1,3) %1行3列元素值都是0的行向量w =0 0 0>> w=rand(1,3) %1行3列元素值是(0,1)之间随机数的行向量w =0.9678 0.6201 0.1560>> rand(3,3)ans =0.3984 0.5437 0.24920.8825 0.4425 0.28510.5390 0.1837 0.5295>> w=randn(1,3) %1行3列元素值符合正态分布的行向量w =0.1556 -0.1822 0.7310>> w=randn(1,3)w =-0.3476 0.2344 -0.8677
>> eye(4) %4维单位矩阵ans =1 0 0 00 1 0 00 0 1 00 0 0 1>> i=eye(4)i =1 0 0 00 1 0 00 0 1 00 0 0 1>> eye(3)ans =1 0 00 1 00 0 1
-1.3绘制图像
w = -6 + sqrt(10)*(randn(1, 10000));>> hist(w) %绘制w的图像
>> hist(w,50) %绘制50条数据的w图像
-1.4帮助函数
>> help eyeeye Identity matrix.eye(N) is the N-by-N identity matrix.eye(M,N) or eye([M,N]) is an M-by-N matrix with 1's onthe diagonal and zeros elsewhere.eye(SIZE(A)) is the same size as A.eye with no arguments is the scalar 1.eye(..., CLASSNAME) is a matrix with ones of class specified byCLASSNAME on the diagonal and zeros elsewhere.eye(..., 'like', Y) is an identity matrix with the same data type, sparsity,and complexity (real or complex) as the numeric variable Y.Note: The size inputs M and N should be nonnegative integers. Negative integers are treated as 0.Example:x = eye(2,3,'int8');See also speye, ones, zeros, rand, randn.eye 的参考页名为 eye 的其他函数>> help randrand Uniformly distributed pseudorandom numbers.R = rand(N) returns an N-by-N matrix containing pseudorandom values drawnfrom the standard uniform distribution on the open interval(0,1). rand(M,N)or rand([M,N]) returns an M-by-N matrix. rand(M,N,P,...) orrand([M,N,P,...]) returns an M-by-N-by-P-by-... array. rand returns ascalar. rand(SIZE(A)) returns an array the same size as A.Note: The size inputs M, N, P, ... should be nonnegative integers.Negative integers are treated as 0.R = rand(..., CLASSNAME) returns an array of uniform values of the specified class. CLASSNAME can be 'double' or 'single'.R = rand(..., 'like', Y) returns an array of uniform values of the same class as Y.The sequence of numbers produced by rand is determined by the settings ofthe uniform random number generator that underlies rand, RANDI, and RANDN.Control that shared random number generator using RNG.Examples:Example 1: Generate values from the uniform distribution on theinterval [a, b].r = a + (b-a).*rand(100,1);Example 2: Use the RANDI function, instead of rand, to generateinteger values from the uniform distribution on the set 1:100.r = randi(100,1,5);Example 3: Reset the random number generator used by rand, RANDI, andRANDN to its default startup settings, so that rand produces the samerandom numbers as if you restarted MATLAB.rng('default')rand(1,5)Example 4: Save the settings for the random number generator used byrand, RANDI, and RANDN, generate 5 values from rand, restore thesettings, and repeat those values.s = rngu1 = rand(1,5)rng(s);u2 = rand(1,5) % contains exactly the same values as u1Example 5: Reinitialize the random number generator used by rand,RANDI, and RANDN with a seed based on the current time. rand willreturn different values each time you do this. NOTE: It is usuallynot necessary to do this more than once per MATLAB session.rng('shuffle');rand(1,5)See Replace Discouraged Syntaxes of rand and randn to use RNG to replacerand with the 'seed', 'state', or 'twister' inputs.See also randi, randn, rng, RandStream, RandStream/rand,sprand, sprandn, randperm.rand 的参考页名为 rand 的其他函数>> help helphelp Display help text in Command Window.help, by itself, lists all primary help topics. Each primary topiccorresponds to a folder name on the MATLAB search path.help NAME displays the help for the functionality specified by NAME,such as a function, operator symbol, method, class, or toolbox.NAME can include a partial path.Some classes require that you specify the package name. Events,properties, and some methods require that you specify the classname. Separate the components of the name with periods, using oneof the following forms:help CLASSNAME.NAMEhelp PACKAGENAME.CLASSNAMEhelp PACKAGENAME.CLASSNAME.NAMEIf NAME is the name of both a folder and a function, help displayshelp for both the folder and the function. The help for a folderis usually a list of the program files in that folder.If NAME appears in multiple folders on the MATLAB path, help displaysinformation about the first instance of NAME found on the path.NOTE:In the help, some function names are capitalized to make them stand out. In practice, type function names in lowercase. Forfunctions that are shown with mixed case (such as javaObject),type the mixed case as shown.EXAMPLES:help close % help for the CLOSE functionhelp database/close % help for CLOSE in the Database Toolboxhelp database % list of functions in the Database Toolbox % and help for the DATABASE functionhelp containers.Map.isKey % help for isKey methodSee also doc, docsearch, lookfor, matlabpath, which.help 的参考页名为 help 的其他函数
二,移动数据
-2.1文件操作
-2.1.1文件基本操作
>> pwd %查看当前文件目录ans =D:\MatlabCode>> cd 'C:\Users\魏振华\Desktop' %进入文件目录:C:\Users\魏振华\Desktop
>> pwdans =C:\Users\魏振华\Desktop>> ls %查看当前文件目录下的文件. wxapp
featureX.dat 腾讯QQ.lnk
priceY.dat 阿里云盘.lnk
-2.1.2文件读入
>> load featureX.dat %读入文件featureX.dat
>> load('featureX.dat') %读入文件featureX.dat
>> load priceY.dat %读入文件priceY.dat
>> who %查看当前工作区的变量您的变量为:A ans featureX priceY sz v >> featureXfeatureX =2104 31600 32400 31416 23000 41987 41534 31427 31380 31494 31940 42000 31890 34478 51268 32300 41320 21236 32609 43031 41458 32625 32200 32637 31839 21000 12040 43137 31811 41437 31239 32132 44215 42162 41664 22238 32567 41200 3852 21852 41203 3>> whos %查看当前工作区的变量及其细则Name Size Bytes Class AttributesA 3x2 48 double ans 1x2 16 double featureX 41x2 656 double priceY 42x1 336 double sz 1x2 16 double v 1x4 32 double
>> clear featureX %从工作区删除featureX
-2.1.3文件导出
>> v=priceY(1:10) %把priceY的前10行赋值给vv =3999329936902320539929993149198921202425>> whosName Size Bytes Class AttributesA 3x2 48 double ans 1x2 16 double featureX 41x2 656 double priceY 42x1 336 double sz 1x2 16 double v 10x1 80 double >> save hello.mat v %把工作区变量v导出为hello.mat
>> clear
>> whos
>> load hello.mat %读入hello.mat文件
>> whosName Size Bytes Class Attributesv 10x1 80 double >> vv =3999329936902320539929993149198921202425>> save hello.txt v -ascii %把工作区变量v导出为hello.txt编码格式为ascii
-2.2矩阵拓展操作
>> A=[1 2;3 4;5 6]A =1 23 45 6>> size(A) %矩阵格式ans =3 2>> sz=size(A)sz =3 2>> size(sz)ans =1 2>> v=[1 2 3 4]v =1 2 3 4>> length(v) %矩阵维度ans =4>> length(A)ans =3>> length([1 2 3 4 5])ans =5>> A=[1 2;3 4;5 6]A =1 23 45 6>> A(3,2)ans =6>> A(2,:) %矩阵第二行数据ans =3 4>> A(:,2) %矩阵第二列数据ans =246>> A([1 3],:) %矩阵第一,第三行数据ans =1 25 6>> A(:,2)=[10; 11; 12] %把矩阵第二列数据修改为[10; 11; 12]A =1 103 115 12>> A=[A,[10;11;12]] %在矩阵A右侧拓展一列[10; 11; 12]A =1 10 103 11 115 12 12>> size(A)ans =3 3>> A(:) %按列优先打印A中元素ans =135101112101112>> A=[1 2;3 4;5 6]A =1 23 45 6>> B=[11 12;13 14;15 16]B =11 1213 1415 16>> C=[A B] %普通合并A,B为C:A在左,B在右C =1 2 11 123 4 13 145 6 15 16>> C=[A;B] %合并A,B为C:A在上,B在下C =1 23 45 611 1213 1415 16>> size(C)ans =6 2>> [A B]ans =1 2 11 123 4 13 145 6 15 16>> [A;B]ans =1 23 45 611 1213 1415 16
三,计算数据
>> A=[1 2;3 4;5 6]A =1 23 45 6>> B=[11 12;13 14;15 16]B =11 1213 1415 16>> C=[1 1;2 2]C =1 12 2>> A*C %矩阵相乘ans =5 511 1117 17>> A.*B %矩阵A,B对应元素相乘ans =11 2439 5675 96>> AA =1 23 45 6>> A.^2 %矩阵A每个元素开方ans =1 49 1625 36>> V=[1;2;3]V =123>> 1./V %1除以每个v中元素ans =1.00000.50000.3333>> 1./Aans =1.0000 0.50000.3333 0.25000.2000 0.1667>> log(V) %对v中每个元素求对数ans =00.69311.0986>> exp(V) %对v中每个元素开e的n次方ans =2.71837.389120.0855>> VV =123>> abs(V) %绝对值函数ans =123>> abs([-1;-2;-3])ans =123>> -Vans =-1-2-3>> V + ones(length(V),1) %v中每个元素加上一个三行一列元素全是1的数ans =234>> ones(3,1) %三行一列元素全是1的矩阵ans =111>> V+ones(3,1)ans =234>> V+1ans =234>> AA =1 23 45 6>> A' %A的转置ans =1 3 52 4 6>> (A')'ans =1 23 45 6>> a=[1 15 2 0.5]a =1.0000 15.0000 2.0000 0.5000>> val=max(a) %最大值函数val =15>> [val,ind]=max(a) %a的最大值及其索引val =15ind =2>> max(A) %A行和列中对应的最大值ans =5 6>> AA =1 23 45 6>> aa =1.0000 15.0000 2.0000 0.5000>> a<3 %依次判断a中元素是否比3小ans =1 0 1 1>> find(a<3) %找出a中比3小的元素所在索引ans =1 3 4>> A=magic(3) %三阶幻方A =8 1 63 5 74 9 2>> help magicmagic Magic square.magic(N) is an N-by-N matrix constructed from the integers1 through N^2 with equal row, column, and diagonal sums.Produces valid magic squares for all N > 0 except N = 2.magic 的参考页>> [r,c]=find(A>=7) %找出A中比7大的元素所在行和列r =132c =123>> A(2,3)ans =7>> help findfind Find indices of nonzero elements.I = find(X) returns the linear indices corresponding to the nonzero entries of the array X. X may be a logical expression. Use IND2SUB(SIZE(X),I) to calculate multiple subscripts from the linear indices I.I = find(X,K) returns at most the first K indices corresponding to the nonzero entries of the array X. K must be a positive integer, but can be of any numeric type.I = find(X,K,'first') is the same as I = find(X,K).I = find(X,K,'last') returns at most the last K indices corresponding to the nonzero entries of the array X.[I,J] = find(X,...) returns the row and column indices instead oflinear indices into X. This syntax is especially useful when workingwith sparse matrices. If X is an N-dimensional array where N > 2, thenJ is a linear index over the N-1 trailing dimensions of X.[I,J,V] = find(X,...) also returns a vector V containing the valuesthat correspond to the row and column indices I and J.Example:A = magic(3)find(A > 5)finds the linear indices of the 4 entries of the matrix A that aregreater than 5.[rows,cols,vals] = find(speye(5))finds the row and column indices and nonzero values of the 5-by-5sparse identity matrix.See also sparse, ind2sub, relop, nonzeros.find 的参考页名为 find 的其他函数>> sum(a)ans =15 15 15>> aa =8 1 63 5 74 9 2>> a=[1 15 2 0.5]a =1.0000 15.0000 2.0000 0.5000>> sum(a)ans =18.5000>> prod(a) %a中各个元素相乘ans =15>> floor(a) %上取整函数ans =1 15 2 0>> ceil(a) %下取整函数ans =1 15 2 1>> rand(3) %元素都是随机数的三阶矩阵ans =0.9153 0.7675 0.29290.4166 0.3721 0.61220.0888 0.4885 0.8492>> val1=rand(3)val1 =0.2734 0.0805 0.52410.0538 0.7115 0.77820.1484 0.0700 0.0715>> val2=rand(3)val2 =0.6613 0.6044 0.19800.4349 0.9963 0.88780.3942 0.1278 0.0602>> val3=max(val1,val2) %取对应元素最大的值作为新的矩阵val3 =0.6613 0.6044 0.52410.4349 0.9963 0.88780.3942 0.1278 0.0715>> AA =8 1 63 5 74 9 2>> max(A,[],1) %按列判断元素最大值ans =8 9 7>> max(A,[],2) %按行判断元素最大值ans =879>> max(max(A)) %求A中所有元素最大值ans =9>> max(A(:)) %求A中所有元素最大值ans =9>> A=magic(9) A =47 58 69 80 1 12 23 34 4557 68 79 9 11 22 33 44 4667 78 8 10 21 32 43 54 5677 7 18 20 31 42 53 55 666 17 19 30 41 52 63 65 7616 27 29 40 51 62 64 75 526 28 39 50 61 72 74 4 1536 38 49 60 71 73 3 14 2537 48 59 70 81 2 13 24 35>> sum(A,1) %求各列元素的和ans =369 369 369 369 369 369 369 369 369>> sum(A,2) %求各行元素的和ans =369369369369369369369369369>> A.*eye(9) ans =47 0 0 0 0 0 0 0 00 68 0 0 0 0 0 0 00 0 8 0 0 0 0 0 00 0 0 20 0 0 0 0 00 0 0 0 41 0 0 0 00 0 0 0 0 62 0 0 00 0 0 0 0 0 74 0 00 0 0 0 0 0 0 14 00 0 0 0 0 0 0 0 35>> sum(sum(A.*eye(9))) %求主对角线元素之和ans =369>> flipud(eye(9)) %反转矩阵ans =0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 1 00 0 0 0 0 0 1 0 00 0 0 0 0 1 0 0 00 0 0 0 1 0 0 0 00 0 0 1 0 0 0 0 00 0 1 0 0 0 0 0 00 1 0 0 0 0 0 0 01 0 0 0 0 0 0 0 0>> sum(sum(A.*flipud(eye(9)))) %求副对角线元素之和ans =369>> A=magic(3)A =8 1 63 5 74 9 2>> pinv(A)ans =0.1472 -0.1444 0.0639-0.0611 0.0222 0.1056-0.0194 0.1889 -0.1028>> temp=pinv(A)temp =0.1472 -0.1444 0.0639-0.0611 0.0222 0.1056-0.0194 0.1889 -0.1028>> temp*Aans =1.0000 0.0000 -0.0000-0.0000 1.0000 0.00000.0000 -0.0000 1.0000>>
四,数据绘制
>> t=[0:0.01:0.98];
>> y1=sin(2*pi*4*t);
>> plot(t,y1) %绘制图像函数
plot()绘制图像函数
>> plot(t,y2) %绘制图像
>> close %关闭图像
>> plot(t,y1) %绘制图像
>> hold on %保存现有图像
>> plot(t,y2,'r') %在现有图像基础上继续绘制图像,图像颜色为红色
>> xlabel('time') %x轴标签
>> ylabel('value') %y轴标签
>> legend('sin','cos') %标记
>> title('my plot') %标题
>> cd 'C:\Users\魏振华\Desktop';print -dpng 'myPlot.png'
%保存图像到桌面,名字为myPlot.png
>> figure(1);plot(t,y1); %用标签1绘制图像
>> figure(2);plot(t,y2); %用标签2绘制图像
>> subplot(1,2,1); %把图像分为两块,现在使用第一块
>> subplot(1,2,2); %把图像分为两块,现在使用第二块
>> subplot(1,2,1);
>> plot(t,y1)
>> subplot(1,2,2);
>> plot(t,y2)
>> sxis([0.5 1 -1 1]) %坐标范围为x轴[0.5 1],y轴[-1 1]
>> clf; %清除画板
>> A=magic(5)A =17 24 1 8 1523 5 7 14 164 6 13 20 2210 12 19 21 311 18 25 2 9>> imagesc(A) %绘制5阶幻方图像
>> imagesc(A),colorbar,colormap gray; %绘制5阶幻方灰度图像
>> imagesc(magic(15)),colorbar,colormap gray; %绘制15阶幻方灰度图像
>> a=1,b=2,c=3a =1b =2c =3
声明:此处a=1,b=2,c=3是同时执行
五,流程控制
-5.1 if语句
>> a=2;
>> if a==1,
disp('the value of a is 1')
elseif(a==2),
disp('the value of a is 2')
else
disp('the value of a is not 1 or 2')
end;
the value of a is 2
-5.2 for语句
表达式1
>> v=zeros(10,1);
>> for i=1:10,
v(i)=2^i;
end;
>> vv =2481632641282565121024
表达式2
>> temp=1:10;
>> for i=temp,
v(i)=100;
end;
>> vv =100100100100100100100100100100
-5.3 while语句
>> i=1;
>> while i<=5,
v(i)=i;
i=i+1;
end;
>> vv =12345100100100100100
-5.4 break语句
break语句
>> i=1;
>> while true,
v(i)=999;
i=i+1;
if i==6,
break;
end;
end;
>> vv =999999999999999100100100100100>>
-5.5 函数
函数文件命名:xxx.m
此处文件名为:squreThisNumber.m
文件内容命名规则
示例1:仅有一个返回值
示例2:有多个返回值
函数文件使用:
方法一:进入函数文件所在文件目录
>> cd 'C:\Users\魏振华\Desktop'
>> squreThisNumber(5)ans =25
方法二:添加函数文件所在目录为搜索目录
此后不管在那个文件之下,均可使用该文件目录的函数
>> addpath('C:\Users\魏振华\Desktop')
>>> squreThisNumber(5)ans =25
例1
>> squreThisNumber(5)ans =25
例2
>> [a,b]=squreAndCubeThisNumber(5);
>> aa =25>> bb =125>>
例3
>> X=[1 1;1 2;1 3]X =1 11 21 3>> y=[1;2;3]y =123>> theta=[0;1]theta =01>> j=costFunction(X,y,theta)j =0
六,矢量
转化方程示例
其中
θ = [ θ 0 θ 1 θ 2 ] θ=\begin{bmatrix} θ_0 \\ θ_1 \\ θ_2 \end{bmatrix} θ= θ0θ1θ2
x = [ x 0 x 1 x 2 ] x=\begin{bmatrix} x_0 \\ x_1 \\ x_2 \end{bmatrix} x= x0x1x2
梯度下降公式
令:
δ = 1 m ∑ i = 1 n ( h θ ( x ( i ) ) − y ( i ) ) 2 \delta=\frac{1}{m}\sum_{i=1}^n(h_θ(x^{(i)})-y^{(i)})^2 δ=m1∑i=1n(hθ(x(i))−y(i))2
δ = [ δ 0 δ 1 δ 2 ] \delta=\begin{bmatrix} \delta_0 \\ \delta_1 \\ \delta_2 \end{bmatrix} δ= δ0δ1δ2
而 δ 0 = 1 m ∑ i = 1 n ( h θ ( x ( i ) ) − y ( i ) ) 2 \delta_0=\frac{1}{m}\sum_{i=1}^n(h_θ(x^{(i)})-y^{(i)})^2 δ0=m1∑i=1n(hθ(x(i))−y(i))2
x i = [ x 0 i x 1 i x 2 i ] x^{i}=\begin{bmatrix} x_0^{i} \\ x_1^{i} \\ x_2^{i} \end{bmatrix} xi= x0ix1ix2i
θ: R n + 1 R^{n+1} Rn+1阶矩阵
δ \delta δ: R n + 1 R^{n+1} Rn+1阶矩阵
x i x^{i} xi: R n + 1 R^{n+1} Rn+1阶矩阵
h θ ( x ( i ) ) − y ( i ) h_θ(x^{(i)})-y^{(i)} hθ(x(i))−y(i)是R上的实数
则转换方程为:θ:=θ- α ⋅ δ \alpha·\delta α⋅δ
本文标签: Matlab基本操作笔记(已完结)
版权声明:本文标题:【Matlab】基本操作笔记(已完结) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1732351791h1533285.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论