zoukankan      html  css  js  c++  java
  • 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial—5.5 控制语句: for, while, if 语句

    5.5 控制语句: forwhileif 语句

      参考视频: 5 - 5 - Control Statements_ for, while, if statements (13 min).mkv

    1、for 循环 通过 index 访问列向量
     1 >> v = zeros(10,1)
     2 v =
     3    0
     4    0
     5    0
     6    0
     7    0
     8    0
     9    0
    10    0
    11    0
    12    0
    13 >> for i = 1 : 10,
    14      v(i) = 2 ^ i;
    15    end;
    16 >> v
    17 v =
    18       2
    19       4
    20       8
    21      16
    22      32
    23      64
    24     128
    25     256
    26     512
    27    1024
    2、for 循环 直接访问列向量元素
     1 >> indices = 1 : 10;
     2 >> indices
     3 indices =
     4     1    2    3    4    5    6    7    8    9   10
     5 >> for i = indices,
     6       disp(i);
     7    end;
     8  1
     9  2
    10  3
    11  4
    12  5
    13  6
    14  7
    15  8
    16  9
    17  10
    3、while 循环访问列向量
     1 >> i = 1;
     2 >> while i <= 5,
     3       v(i) = 100;
     4       i = i + 1;
     5    end;
     6 >> v
     7 v =
     8     100
     9     100
    10     100
    11     100
    12     100
    13      64
    14     128
    15     256
    16     512
    17    1024
    4、while(true) 和 break
     1 >> i = 1 ;
     2 >> while(true),
     3      v(i) = 999;
     4      i = i + 1;
     5      if i == 6,
     6         break;
     7      end;
     8    end;
     9 >>
    10 >> v
    11 v =
    12     999
    13     999
    14     999
    15     999
    16     999
    17      64
    18     128
    19     256
    20     512
    21    1024
    5、if else 语句
    1 >> v(1) = 2;
    2 >> if v(1) == 1,
    3       disp('The value is one');
    4    elseif v(1) == 2,
    5       disp('The value is two');
    6    else,
    7       disp('The value is not one or two');
    8    end;
    9 The value is two
    6、自定义函数 function

      定义函数 squareThisNumber(x),内容如下: 

    1 function y = squareThisNumber(x)
    2   y = x^2;
    3 endfunction

      将函数保存为squarethisnumber.m,注意此时是小写。

      这时候调用函数 squareThisNumber(2) 提示找不到函数

    1 >> squareThisNumber(2);
    2 error: 'squareThisNumber' undefined near line 1 column 1
    3 >>
    4 >> ls
    5 [.]                         featuresX.dat               priceY.dat
    6 [..]                        hello.dat                   squarethisnumber.m

      将文件命名为函数一致squareThisNumber.m(大小写也一致),这时候调用函数成功

    1 >> ls
    2 [.]                         featuresX.dat               priceY.dat
    3 [..]                        hello.dat                   squareThisNumber.m
    4 >> squareThisNumber(2);
    5 >> a = squareThisNumber(2);
    6 >> a
    7 a =  4

      这说明:Octave 是大小写敏感的,文件名必须和函数名大小写一致。

    7、多个返回值的函数

      Octave 函数有和其他语言不一样的地方是可以返回多个值。定义方法squareAndCubeThisNumber(x)如下:

    1 function [y1, y2] = squareAndCubeThisNumber(x),
    2   y1 = x ^ 2;
    3   y2 = x ^ 3;
    4 endfunction

      调用:

    1 >> a = squareAndCubeThisNumber(2)  % 接受了第一个返回值
    2 a =  4
    3 >> [a, b] = squareAndCubeThisNumber(2)  % 接受两个返回值
    4 a =  4
    5 b =  8
    8、更复杂的函数

      保存下面的函数到costFunctionJ.m中

     1 function J = costFunctionJ(X, y, theta),
     2   % X is the "design matrix" containing our training examples
     3   % y is the class labels
     4   
     5   m = size(X, 1);   % number of training examples, size of rows
     6   predictions = X * theta; % predictions of hapothesis on all m examples
     7   sqrErrors = (predictions - y) .^ 2; % squared errors .^ 指的是对数据中每个元素平方
     8   
     9   J = 1 / (2 * m) * sum(sqrErrors); 
    10   
    11 endfunction

      针对上边的数据集初始化矩阵。调用函数,计算代价函数的值。

    1 >> X = [1 1; 1 2 ; 1 3];
    2 >> y = [1;2;3];
    3 >> theta = [0;1];      % Θ为0,1  h(x)=x  此时完全拟合数据,代价函数的值为0
    4 >> j = costFunctionJ(X,y,theta)
    5 j = 0
    1 >> theta = [0;0];      % Θ 为0,0 h(x)=0 此时不能拟合数据
    2 >> j = costFunctionJ(X,y,theta)
    3 j =  2.3333
    4 
    5 >> (1^2 + 2^2 + 3^2) / (2*3)  % 代价函数的值
    6 ans =  2.3333
  • 相关阅读:
    高精度乘法
    阶乘
    高精度减法
    高精度加法
    曹冲养猪
    采药2
    nginx.conf详解
    系统盘脚本扩容
    IDEA中编写脚本并运行shell脚本
    常用的pdf工具
  • 原文地址:https://www.cnblogs.com/maxiaodoubao/p/9873191.html
Copyright © 2011-2022 走看看