5.5 控制语句: for, while, if 语句

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

1、for 循环 通过 index 访问列向量
 >> v = zeros(,)
v = >> for i = : ,
v(i) = ^ i;
end;
>> v
v =
2、for 循环 直接访问列向量元素
 >> indices =  : ;
>> indices
indices = >> for i = indices,
disp(i);
end;
3、while 循环访问列向量
 >> i = ;
>> while i <= ,
v(i) = ;
i = i + ;
end;
>> v
v =
4、while(true) 和 break
 >> i =  ;
>> while(true),
v(i) = ;
i = i + ;
if i == ,
break;
end;
end;
>>
>> v
v =
5、if else 语句
 >> v() = ;
>> if v() == ,
disp('The value is one');
elseif v() == ,
disp('The value is two');
else,
disp('The value is not one or two');
end;
The value is two
6、自定义函数 function

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

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

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

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

 >> squareThisNumber();
error: 'squareThisNumber' undefined near line column
>>
>> ls
[.] featuresX.dat priceY.dat
[..] hello.dat squarethisnumber.m

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

 >> ls
[.] featuresX.dat priceY.dat
[..] hello.dat squareThisNumber.m
>> squareThisNumber();
>> a = squareThisNumber();
>> a
a =

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

7、多个返回值的函数

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

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

  调用:

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

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

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

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

 >> X = [ ;   ;  ];
>> y = [;;];
>> theta = [;];      % Θ为0,1 h(x)=x 此时完全拟合数据,代价函数的值为0
>> j = costFunctionJ(X,y,theta)
j =
 >> theta = [;];      % Θ 为0,0 h(x)=0 此时不能拟合数据
>> j = costFunctionJ(X,y,theta)
j = 2.3333 >> (^ + ^ + ^) / (*)  % 代价函数的值
ans = 2.3333

【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial—5.5 控制语句: for, while, if 语句的更多相关文章

  1. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial

    Lecture 5 Octave教程 5.1 基本操作 Basic Operations 5.2 移动数据 Moving Data Around 5.3 计算数据 Computing on Data ...

  2. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial—5.6 向量化 Vectorization

    5.6 向量化 Vectorization 参考视频: 5 - 6 - Vectorization (14 min).mkv 下面是向量化的小例子,如果将所有u(j) .所有v(j).所有w(j)都看 ...

  3. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial—5.4 绘制数据图

    5.4 绘制数据图 参考视频: 5 - 4 - Plotting Data (10 min) 5.4.1 绘制曲线 1.画一个sin曲线 >> t = [:0.01:0.98]; > ...

  4. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 15—Anomaly Detection异常检测

    Lecture 15 Anomaly Detection 异常检测 15.1 异常检测问题的动机 Problem Motivation 异常检测(Anomaly detection)问题是机器学习算法 ...

  5. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 16—Recommender Systems 推荐系统

    Lecture 16 Recommender Systems 推荐系统 16.1 问题形式化 Problem Formulation 在机器学习领域,对于一些问题存在一些算法, 能试图自动地替你学习到 ...

  6. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 14—Dimensionality Reduction 降维

    Lecture 14 Dimensionality Reduction 降维 14.1 降维的动机一:数据压缩 Data Compression 现在讨论第二种无监督学习问题:降维. 降维的一个作用是 ...

  7. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 13—Clustering 聚类

    Lecture 13 聚类 Clustering 13.1 无监督学习简介  Unsupervised Learning Introduction 现在开始学习第一个无监督学习算法:聚类.我们的数据没 ...

  8. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 12—Support Vector Machines 支持向量机

    Lecture 12 支持向量机 Support Vector Machines 12.1 优化目标 Optimization Objective 支持向量机(Support Vector Machi ...

  9. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 11—Machine Learning System Design 机器学习系统设计

    Lecture 11—Machine Learning System Design 11.1 垃圾邮件分类 本章中用一个实际例子: 垃圾邮件Spam的分类 来描述机器学习系统设计方法.首先来看两封邮件 ...

随机推荐

  1. CSS: transitions

    CSS Transitions CSS transitions allows you to change property values smoothly (from one value to ano ...

  2. CSS: The resize Property

    用户手动调节输入框样式: <!DOCTYPE html> <html> <head> <style> div { border: 2px solid; ...

  3. MySqlDBHelper数据库连接

    这里是本人在工作中用到,希望给大家帮助 public class MySqlDBHelper { //获取一个记录器 private static readonly log4net.ILog log ...

  4. Kali Linux安装SSH Server

    Kali Linux默认并没有安装SSH服务,为了实现远程登录Kali Linux,我们需要安装SSH服务. 安装 OpenSSH Server # apt-get install openssh-s ...

  5. I.MX6 recovery mode hacking

    /******************************************************************************** * I.MX6 recovery m ...

  6. 深入理解java虚拟机-第三章

    1.概述 2.对象已死吗? 引用计数器 给对象添加一个引用计数器,每当有引用时,计数器加1,引用失效时,计数器减1:任何时刻计数器为0的对象就是不可能再被使用的. 若对象是循环引用,则无法处理.JVM ...

  7. 浅析Python中bytes和str区别

    本博转载自:Chown-Jane-Y的浅析Python3中的bytes和str类型 Python 3最重要的新特性之一是对字符串和二进制数据流做了明确的区分.文本总是Unicode,由str类型表示, ...

  8. 怎么用HD Tune检测硬盘坏道

    HD Tune软件不仅小巧而且很易使用,是一款检测电脑硬盘的优良工具.不仅是电脑硬盘,包括移动硬盘在内一样可以检测.那么,如何使用HD Tune呢?如何使用HD Tune检测磁盘坏道呢? 工具/原料 ...

  9. 【java规则引擎】规则引擎RuleBase中利用观察者模式

    (1)当RuleBase中有规则添加或删除,利用观察者模式实现,一旦有变动,规则引擎其他组件也做出相应的改变.(2)学习思想:当一个应用中涉及多个组件,为了实现易扩展,解耦思想.可以利用观察者模式实现 ...

  10. Linux多进程多线程例子

    看了apue3,关于进程线程和进程间通信写了一个例子,方便自己理解相关知识,备忘. #include <stdlib.h> #include <stdio.h> #includ ...