zoukankan      html  css  js  c++  java
  • [Machine Learning] Octave Control Statements, for while if

    For:

    v = zeros(10, 1);
    
    for i=1:10,
        v(i) = 2^i;
    end;
    
    # the same as
    
    indices=1:10
    for i=indices,
        disp(i)
    end;

    while & if & break:

    i=1;
    while i <=5,
        v(i) = 100;
        i = i + 1;
        if i ==6,
           break;
        end;
    end;    

    if & elseif:

    if v(1) ==1,
        disp(The value is one')
    elseif v(1) == 2,
        disp('The value is two'')
    else
        disp('The value is not one or two');
    end;

    function:

    For exmaple, you have a file called 'squareThisNumber.m' , inside the file, there is a function:

    function y = squareThisNumber(x)
    
    y =x ^ 2;

    function can return multi values:

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

    Call the function get the result:

    [a,b] = squareAndCubeThisNumber(5);
    a # 25
    b # 125

    Example:

    X = [1 1; 1 2; 1 3];
    y = [1; 2; 3];
    theta = [0;1];
    
    j = costFunction(X, y, theta)
    
    
    function J = costFunctionJ(X, y, theta)
    
    m = size(X, 1) # number of training examples
    predictions = X*theta; # predictions of hypothesis on all m examples
    sqrErrors = (predictions-y).^2;
    
    J = 1/(2*m) * sum(sqrErrors);

    Search path:

    If you want to run that file, but you local in a different directory, you need to add that directory into your search path:

    addpath('/usr/xxxx/octave') # add the directory where the file locate
    squreThisNumber(5)
  • 相关阅读:
    BZOJ BLO 1123 (割点)【双连通】
    P4291 [HAOI2008]排名系统
    P3165 [CQOI2014]排序机械臂
    P3224 [HNOI2012]永无乡
    P1169 [ZJOI2007]棋盘制作
    P2303 [SDOi2012]Longge的问题
    P2216 [HAOI2007]理想的正方形
    P2473 [SCOI2008]奖励关
    P2617 Dynamic Rankings
    P2518 [HAOI2010]计数
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13527068.html
Copyright © 2011-2022 走看看