第五课 控制语句和方程 For,while,if statements and functions

(1)For loop

v=zeros(10,1) %initial vectors

for i=1:10, %Assign for the vectors

v(i) = 2^i;

end;

v

(%You can also do that:

indices=1:10;

for i=indices,

v(i)=2^i;

end;

v

)

(2)whileloop

I = 1;

while I <= 5,

v(i) = 100;

I = i+1;

end;

v

(3)break statememts

i=1;

while true,

v(i) = 999;

I = i+1;

if I == 6,

break;

end;

end;

(4)if-else statements

v(1) = 2;

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;

(5)Funtions

1:桌面上有一个名为“squareAndCubeThisNumber.m”的文件,内容如下:

function [y1,y2] = squareAndCubeThisNumber(x)

y = x^2;

y = x^3;

要调用此函数,方法如下:

(1)输入pwd,查看当前文件所在路径,若不在和“squareThisNumber.m”文件的同一目录下,有两种方法进入同一目录:

1)cd /home/flipped/Desktop

2)addpath(' /home/flipped/Desktop') %Octave search path (advanced/optional)

(2)调用 squareThisNumber函数,输入如下:

quareThisNumber(5)

从此例还可看出,Octave不同于其他语言的一点是,函数可以返回两个及两个以上的值。

2:通过少量数据集计算其成本函数

桌面上有一个名为“costFuctionJ.m”的文件,内容如下:

unction J = costFuctionJ(X, y, thera)

m = size(X,1);

predictions = X*thera;

sqrErrors = (predictions-y).^2;

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

设定 X = [1 1;1 2;1 3](第一列均为x0值,第二列为样本集x1,x2,x3的值)

y = [1;2;3](样本集中y的值)

thera = [0;1];(假设当x0为0时,y为0;x1为1时,y为1)

运行函数得结果等于0. (若设thera = [0,0],运行函数结果为2.3333.)

向量化 Vectorization

优点:(1)利用各种语言中经过高度优化的代数库会使你的代码运行速度更快,更有效。(2)这也意味着你可以用更少的代码实现一些功能。

Octave Tutorial(《Machine Learning》)之第五课《控制语句和方程及向量化》的更多相关文章

  1. 【机器学习Machine Learning】资料大全

    昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...

  2. How do I learn machine learning?

    https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644   How Can I Learn X? ...

  3. Octave Tutorial(《Machine Learning》)之第一课《数据表示和存储》

    Octave Tutorial 第一课 Computation&Operation 数据表示和存储 1.简单的四则运算,布尔运算,赋值运算(a && b,a || b,xor( ...

  4. Machine Learning – 第2周(Linear Regression with Multiple Variables、Octave/Matlab Tutorial)

    Machine Learning – Coursera Octave for Microsoft Windows GNU Octave官网 GNU Octave帮助文档 (有900页的pdf版本) O ...

  5. Coursera machine learning 第二周 quiz 答案 Octave/Matlab Tutorial

    https://www.coursera.org/learn/machine-learning/exam/dbM1J/octave-matlab-tutorial Octave Tutorial 5  ...

  6. Tutorial: Create a Windows Machine Learning UWP application (C#)

    In this tutorial, we'll build a simple Universal Windows Platform application that uses a trained ma ...

  7. 第五周(web,machine learning笔记)

    2019/11/2 1.    表现层状态转换(REST, representational state transfer.)一种万维网软件架构风格,目的是便于不同软件/程序在网络(例如互联网)中互相 ...

  8. 吴恩达《深度学习》-课后测验-第三门课 结构化机器学习项目(Structuring Machine Learning Projects)-Week1 Bird recognition in the city of Peacetopia (case study)( 和平之城中的鸟类识别(案例研究))

    Week1 Bird recognition in the city of Peacetopia (case study)( 和平之城中的鸟类识别(案例研究)) 1.Problem Statement ...

  9. Machine Learning - 第3周(Logistic Regression、Regularization)

    Logistic regression is a method for classifying data into discrete outcomes. For example, we might u ...

随机推荐

  1. MAC的VIMRC

    set nocompatible            " 关闭 vi 兼容模式 syntax on                   " 自动语法高亮 " color ...

  2. css3 2d转换3d转换以及动画的知识点汇总

    css3 2d转换 2d转换的方法: 1.移动 translate(x, y) 可以改变元素的位置,x.y可为负值: 2.缩放 scale(x, y) 可以对元素进行水平和垂直方向的缩放,x.y的取值 ...

  3. 设备文件三大结构:inode,file,file_operations

    驱动程序就是向下控制硬件,向上提供接口,这里的向上提供的接口最终对应到应用层有三种方式:设备文件,/proc,/sys,其中最常用的就是使用设备文件,而Linux设备中用的最多的就是字符设备,本文就以 ...

  4. WinForm 更换主窗体的例子

    做一个登录窗口,登录成功时关闭form1,展示from2界面 1.主界面Login namespace WindowsFormsApplication1 { public partial class ...

  5. 有关C#分部类型的一些叙述

    等待着元宵节的到来,过完元宵,这个年也算是过完了,也得开始出去挣钱了,过年回家感觉每个人都觉得很牛,只有自己太渣,为了避免年底再出现这样尴尬的局面,还是需要努力干活.争取当上CEO,赢取白富美,走上人 ...

  6. We Chall-Encodings: URL -Writeup

    MarkdownPad Document html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,ab ...

  7. Swift 2.0 UIAlertView 和 UIActionSheet 的使用

    在 IOS 9.0 之后, UIAlertView  是 给废弃了的,虽然你要使用的话,暂时还是可以的,但是在 9.0 之后,IOS 推荐大家使用的是  UIAlertController 这个控制器 ...

  8. 开放才能进步!Angular和Wijmo一起走过的日子

    Angular 已成为广受欢迎的前端框架.去年9月份,期待已久的 Angular2 的正式版发布之后,Angular 又迎来了新一轮热潮.伴随着 Angular 这一路走来,Wijmo 一直都是第一个 ...

  9. ConOS安装mysql5.7 及简单配置

    安装 保证你的用户有权限 安装  没有 切换 root su root  (su的意思:swich user) # rpm -ivh http://dev.mysql.com/get/mysql57- ...

  10. pyqt的 .ui 转换为 .py 后的操作注意事项

    1. 增加 import sys 2. 将 Ui_MainWindow(object) 中的 object 修改成修改成 QtGui.QMainWindow 3. 在 Ui_MainWindow 类中 ...