【Matlab】基本操作笔记(已完结)

引言

无论是Octave还是Matlab,矩阵运算都是按照列优先

注:此处用的是Matlab2016a编码

一,基本操作

-1.1普通数据操作

>> 5+6

ans =

    11

>> 3-2

ans =

     1

>> 5*8

ans =

    40

>> 1/2

ans =

    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=3

a =

     3

>> a=3; %如果加上;控制台就无法打印数据

>> disp(a) %打印数据a
     3

>> 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
>> a

a =

    3.1416

>> format long  %更改数据格式long
>> a

a =

   3.141592653589793

>> format short %更改数据格式为short
>> a

a =

    3.1416
>>clear   %清除工作区数据 
>>clc	  %清除命令行数据

-1.2矩阵操作

>> A=[1 2;3 4;5 6]

A =

     1     2
     3     4
     5     6

>> A=[1 2;
3 4;
5 6]

A =

     1     2
     3     4
     5     6

>> v=[1 2 3]

v =

     1     2     3

>> v=[1;2;3]

v =

     1
     2
     3

>> v=1:0.1:2 %从1到2,步伐为0.1

v =

  181.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000

  9111.8000    1.9000    2.0000

>> v=1:6 %从1到6步伐为1

v =

     1     2     3     4     5     6

>> ones(2,3) %2行3列元素值都是1的矩阵

ans =

     1     1     1
     1     1     1

>> c=2*ones(2,3)

c =

     2     2     2
     2     2     2

>> c=[2 2 2;2 2 2]

c =

     2     2     2
     2     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.2492
    0.8825    0.4425    0.2851
    0.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     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

>> i=eye(4)

i =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

>> eye(3)

ans =

     1     0     0
     0     1     0
     0     0     1

-1.3绘制图像

 w = -6 + sqrt(10)*(randn(1, 10000));
 >> hist(w) %绘制w的图像

请添加图片描述

>> hist(w,50) %绘制50条数据的w图像

请添加图片描述

-1.4帮助函数

>> help eye
 eye 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 on
    the 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 by
    CLASSNAME 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 rand
 rand Uniformly distributed pseudorandom numbers.
    R = rand(N) returns an N-by-N matrix containing pseudorandom values drawn
    from 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,...) or
    rand([M,N,P,...]) returns an M-by-N-by-P-by-... array.  rand returns a
    scalar.  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 of
    the 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 the
       interval [a, b].
          r = a + (b-a).*rand(100,1);
 
       Example 2: Use the RANDI function, instead of rand, to generate
       integer 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, and
       RANDN to its default startup settings, so that rand produces the same
       random numbers as if you restarted MATLAB.
          rng('default')
          rand(1,5)
 
       Example 4: Save the settings for the random number generator used by
       rand, RANDI, and RANDN, generate 5 values from rand, restore the
       settings, and repeat those values.
          s = rng
          u1 = rand(1,5)
          rng(s);
          u2 = rand(1,5) % contains exactly the same values as u1
 
       Example 5: Reinitialize the random number generator used by rand,
       RANDI, and RANDN with a seed based on the current time.  rand will
       return different values each time you do this.  NOTE: It is usually
       not 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 replace
    rand with the 'seed', 'state', or 'twister' inputs.
 
    See also randi, randn, rng, RandStream, RandStream/rand,
             sprand, sprandn, randperm.

    rand 的参考页
    名为 rand 的其他函数

>> help help
   help Display help text in Command Window.
      help, by itself, lists all primary help topics. Each primary topic
      corresponds 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 class
      name. Separate the components of the name with periods, using one
      of the following forms:
 
          help CLASSNAME.NAME
          help PACKAGENAME.CLASSNAME
          help PACKAGENAME.CLASSNAME.NAME
 
      If NAME is the name of both a folder and a function, help displays
      help for both the folder and the function. The help for a folder
      is usually a list of the program files in that folder.
 
      If NAME appears in multiple folders on the MATLAB path, help displays
      information 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. For
      functions that are shown with mixed case (such as javaObject),
      type the mixed case as shown.
 
      EXAMPLES:
 
      help close           % help for the CLOSE function
      help database/close  % help for CLOSE in the Database Toolbox
      help database        % list of functions in the Database Toolbox 
                           % and help for the DATABASE function
      help containers.Map.isKey   % help for isKey method
 
      See also doc, docsearch, lookfor, matlabpath, which.

    help 的参考页
    名为 help 的其他函数

二,移动数据

-2.1文件操作

-2.1.1文件基本操作

>> pwd %查看当前文件目录

ans =

D:\MatlabCode

>> cd 'C:\Users\魏振华\Desktop' %进入文件目录:C:\Users\魏振华\Desktop
>> pwd

ans =

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         

>> featureX

featureX =

        2104           3
        1600           3
        2400           3
        1416           2
        3000           4
        1987           4
        1534           3
        1427           3
        1380           3
        1494           3
        1940           4
        2000           3
        1890           3
        4478           5
        1268           3
        2300           4
        1320           2
        1236           3
        2609           4
        3031           4
        1458           3
        2625           3
        2200           3
        2637           3
        1839           2
        1000           1
        2040           4
        3137           3
        1811           4
        1437           3
        1239           3
        2132           4
        4215           4
        2162           4
        1664           2
        2238           3
        2567           4
        1200           3
         852           2
        1852           4
        1203           3
         
>> whos	%查看当前工作区的变量及其细则
  Name           Size            Bytes  Class     Attributes

  A              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行赋值给v

v =

        3999
        3299
        3690
        2320
        5399
        2999
        3149
        1989
        2120
        2425

>> whos
  Name           Size            Bytes  Class     Attributes

  A              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文件
>> whos
  Name       Size            Bytes  Class     Attributes

  v         10x1                80  double              

>> v

v =

        3999
        3299
        3690
        2320
        5399
        2999
        3149
        1989
        2120
        2425

>> save hello.txt v -ascii	%把工作区变量v导出为hello.txt编码格式为ascii

-2.2矩阵拓展操作

>> A=[1 2;3 4;5 6]

A =

     1     2
     3     4
     5     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     2
     3     4
     5     6

>> A(3,2)

ans =

     6

>> A(2,:)	%矩阵第二行数据

ans =

     3     4

>> A(:,2)	%矩阵第二列数据

ans =

     2
     4
     6

>> A([1 3],:)	%矩阵第一,第三行数据

ans =

     1     2
     5     6

>> A(:,2)=[10; 11; 12]	%把矩阵第二列数据修改为[10; 11; 12]

A =

     1    10
     3    11
     5    12

>> A=[A,[10;11;12]]	%在矩阵A右侧拓展一列[10; 11; 12]

A =

     1    10    10
     3    11    11
     5    12    12

>> size(A)

ans =

     3     3

>> A(:)	%按列优先打印A中元素

ans =

     1
     3
     5
    10
    11
    12
    10
    11
    12

>> A=[1 2;3 4;5 6]

A =

     1     2
     3     4
     5     6

>> B=[11 12;13 14;15 16]

B =

    11    12
    13    14
    15    16

>> C=[A B] %普通合并A,B为C:A在左,B在右

C =

     1     2    11    12
     3     4    13    14
     5     6    15    16

>> C=[A;B]	%合并A,B为C:A在上,B在下

C =

     1     2
     3     4
     5     6
    11    12
    13    14
    15    16

>> size(C)

ans =

     6     2

>> [A B]

ans =

     1     2    11    12
     3     4    13    14
     5     6    15    16

>> [A;B]

ans =

     1     2
     3     4
     5     6
    11    12
    13    14
    15    16

三,计算数据

>> A=[1 2;3 4;5 6]

A =

     1     2
     3     4
     5     6

>> B=[11 12;13 14;15 16]

B =

    11    12
    13    14
    15    16

>> C=[1 1;2 2]

C =

     1     1
     2     2

>> A*C	%矩阵相乘

ans =

     5     5
    11    11
    17    17

>> A.*B	%矩阵A,B对应元素相乘

ans =

    11    24
    39    56
    75    96

>> A

A =

     1     2
     3     4
     5     6

>> A.^2	%矩阵A每个元素开方

ans =

     1     4
     9    16
    25    36

>> V=[1;2;3]

V =

     1
     2
     3


>> 1./V	%1除以每个v中元素

ans =

    1.0000
    0.5000
    0.3333

>> 1./A

ans =

    1.0000    0.5000
    0.3333    0.2500
    0.2000    0.1667

>> log(V)	%对v中每个元素求对数

ans =

         0
    0.6931
    1.0986

>> exp(V)	%对v中每个元素开e的n次方

ans =

    2.7183
    7.3891
   20.0855

>> V

V =

     1
     2
     3

>> abs(V)	%绝对值函数

ans =

     1
     2
     3

>> abs([-1;-2;-3])

ans =

     1
     2
     3

>> -V

ans =

    -1
    -2
    -3


>> V + ones(length(V),1)	%v中每个元素加上一个三行一列元素全是1的数

ans =

     2
     3
     4

>> ones(3,1)	%三行一列元素全是1的矩阵

ans =

     1
     1
     1


>> V+ones(3,1)

ans =

     2
     3
     4

>> V+1

ans =

     2
     3
     4

>> A

A =

     1     2
     3     4
     5     6


 
>> A'	%A的转置

ans =

     1     3     5
     2     4     6

>> (A')'

ans =

     1     2
     3     4
     5     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 =

    15


ind =

     2

>> max(A)	%A行和列中对应的最大值

ans =

     5     6

>> A

A =

     1     2
     3     4
     5     6

>> a

a =

    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     6
     3     5     7
     4     9     2

>> help magic
 magic  Magic square.
    magic(N) is an N-by-N matrix constructed from the integers
    1 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 =

     1
     3
     2


c =

     1
     2
     3

>> A(2,3)

ans =

     7

>> help find
 find   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 of
    linear indices into X. This syntax is especially useful when working
    with sparse matrices.  If X is an N-dimensional array where N > 2, then
    J is a linear index over the N-1 trailing dimensions of X.
 
    [I,J,V] = find(X,...) also returns a vector V containing the values
    that 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 are
    greater than 5.
 
       [rows,cols,vals] = find(speye(5))
 
    finds the row and column indices and nonzero values of the 5-by-5
    sparse identity matrix.
 
    See also sparse, ind2sub, relop, nonzeros.

    find 的参考页
    名为 find 的其他函数

>> sum(a)

ans =

    15    15    15

>> a

a =

     8     1     6
     3     5     7
     4     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.2929
    0.4166    0.3721    0.6122
    0.0888    0.4885    0.8492


>> val1=rand(3)

val1 =

    0.2734    0.0805    0.5241
    0.0538    0.7115    0.7782
    0.1484    0.0700    0.0715

>> val2=rand(3)

val2 =

    0.6613    0.6044    0.1980
    0.4349    0.9963    0.8878
    0.3942    0.1278    0.0602

>> val3=max(val1,val2)	%取对应元素最大的值作为新的矩阵

val3 =

    0.6613    0.6044    0.5241
    0.4349    0.9963    0.8878
    0.3942    0.1278    0.0715

>> A

A =

     8     1     6
     3     5     7
     4     9     2

>> max(A,[],1)	%按列判断元素最大值

ans =

     8     9     7

>> max(A,[],2)	%按行判断元素最大值

ans =

     8
     7
     9



>> max(max(A))	%求A中所有元素最大值

ans =

     9

>> max(A(:))	%求A中所有元素最大值

ans =

     9

>> A=magic(9)	

A =

    47    58    69    80     1    12    23    34    45
    57    68    79     9    11    22    33    44    46
    67    78     8    10    21    32    43    54    56
    77     7    18    20    31    42    53    55    66
     6    17    19    30    41    52    63    65    76
    16    27    29    40    51    62    64    75     5
    26    28    39    50    61    72    74     4    15
    36    38    49    60    71    73     3    14    25
    37    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 =

   369
   369
   369
   369
   369
   369
   369
   369
   369

>> A.*eye(9)	

ans =

    47     0     0     0     0     0     0     0     0
     0    68     0     0     0     0     0     0     0
     0     0     8     0     0     0     0     0     0
     0     0     0    20     0     0     0     0     0
     0     0     0     0    41     0     0     0     0
     0     0     0     0     0    62     0     0     0
     0     0     0     0     0     0    74     0     0
     0     0     0     0     0     0     0    14     0
     0     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     1
     0     0     0     0     0     0     0     1     0
     0     0     0     0     0     0     1     0     0
     0     0     0     0     0     1     0     0     0
     0     0     0     0     1     0     0     0     0
     0     0     0     1     0     0     0     0     0
     0     0     1     0     0     0     0     0     0
     0     1     0     0     0     0     0     0     0
     1     0     0     0     0     0     0     0     0



>> sum(sum(A.*flipud(eye(9))))	%求副对角线元素之和

ans =

   369

>> A=magic(3)

A =

     8     1     6
     3     5     7
     4     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*A

ans =

    1.0000    0.0000   -0.0000
   -0.0000    1.0000    0.0000
    0.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    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    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=3

a =

     1


b =

     2


c =

     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;
>> v

v =

           2
           4
           8
          16
          32
          64
         128
         256
         512
        1024

表达式2

>> temp=1:10;
>> for i=temp,
v(i)=100;
end;
>> v

v =

   100
   100
   100
   100
   100
   100
   100
   100
   100
   100

-5.3 while语句

>> i=1;
>> while i<=5,
v(i)=i;
i=i+1;
end;
>> v

v =

     1
     2
     3
     4
     5
   100
   100
   100
   100
   100

-5.4 break语句

break语句

>> i=1;
>> while true,
v(i)=999;
i=i+1;
if i==6,
break;
end;
end;
>> v

v =

   999
   999
   999
   999
   999
   100
   100
   100
   100
   100

>> 

-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);
>> a

a =

    25

>> b

b =

   125

>> 

例3
请添加图片描述

>> X=[1 1;1 2;1 3]

X =

     1     1
     1     2
     1     3

>> y=[1;2;3]

y =

     1
     2
     3

>> theta=[0;1]

theta =

     0
     1

>> 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 δ=m1i=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=m1i=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 αδ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

君问归期魏有期

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值