Octave /Matlab--Control Statements:for,while, if statement----Coursera ML笔记

>> v = zeros(10,1)
v =
     0
     0
     0
     0
     0
     0
     0
     0
     0

     0

>> for i=1:10;
         v(i) = 2^i;
      end;
>> v
v =
           2
           4
           8
          16
          32
          64
         128
         256
         512
        1024

>> indices = 1:10;
>> indices
indices =

  Columns 1 through 9

     1     2     3     4     5     6     7     8     9

  Column 10

    10

>> for i = indices,
       disp(i);
   end;
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10

>> v

v =
           2
           4
           8
          16
          32
          64
         128
         256
         512
        1024


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

>> v

v =
         100
         100
         100
         100
         100
          64
         128
         256
         512
        1024

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

>> v

v =
         999
         999
         999
         999
         999
          64
         128
         256
         512
        1024

>>if ****,

          ######;

     elseif ****,

         #######;

     else,

         ######;

     end;

=============================================================

拟合上图训练集合

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

>>y = [1; 2; 3]

>>theta = [0;1]

function J = costFunctionJ(X, y, thera)

% X is the "design matrix" containing our training examples,

%y is the class labels

m = size(X, 1)    %number of training examples

predictions = X*thera;  % predictions of hypothesis on all m examples

sqrErrors = (predictions - y).^2;

J = 1/(2*m) *sum(sqrErrors);

原文地址:https://www.cnblogs.com/judejie/p/9008921.html