第五课 控制语句和方程 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. css3弹性盒子模型——回顾。

    1.box-flex属性规定框的子元素是否可伸缩其尺寸. 父元素必须要声明display:box;子元素才可以用box-flex. 语法:box-flex:value; 示例: <style&g ...

  2. 去除android或者iOS系统默认的一些样式总结

    ios系统中元素被触摸时产生的半透明灰色遮罩怎么去掉 iOS用户点击一个链接,会出现一个半透明灰色遮罩, 如果想要禁用,可设置-webkit-tap-highlight-color的alpha值为0, ...

  3. 框架篇:Spring+SpringMVC+hibernate整合开发

    前言: 最近闲的蛋疼,搭个框架写成博客记录下来,拉通一下之前所学知识,顺带装一下逼. 话不多说,我们直接步入正题. 准备工作: 1/ IntelliJIDEA的安装配置:jdk/tomcat等..(本 ...

  4. Android EventBus 3.0 实例使用详解

    EventBus的使用和原理在网上有很多的博客了,其中泓洋大哥和启舰写的非常非常棒,我也是跟着他们的博客学会的EventBus,因为是第一次接触并使用EventBus,所以我写的更多是如何使用,源码解 ...

  5. 欢迎来到Devil_lixu的技术博客

    Welcome to my blog 如果你觉得内容对你还有一点点的帮助,那就果断点赞吧^_^

  6. JAVA中的数据结构 - 真正的去理解红黑树

    一, 红黑树所处数据结构的位置: 在JDK源码中, 有treeMap和JDK8的HashMap都用到了红黑树去存储 红黑树可以看成B树的一种: 从二叉树看,红黑树是一颗相对平衡的二叉树 二叉树--&g ...

  7. [solr] - solr5.2.1环境搭建 - 使用solr自带的jetty服务器

    solr除了使用tomcat作为容器外,还可以使用其他的常用容器,比如jetty,jboos等,而且solr自身就自带jetty服务器,那么现在就solr自带的jetty服务器进行安装操作.由于sol ...

  8. java 四则混合运算 计算器

    public class Counter { /**用递归算法,把括号内的公式算出然后递归   * @param args   */ public static void calculator (St ...

  9. groovy hello world

    安装方法见官方文档http://groovy.codehaus.org/Installing+Groovy 用新一个文件HelloWorld.groovy,以utf8的编码保存,内容为: printl ...

  10. Redis服务器搭建

    下载并解压redis,然后进入redis所在目录   编译安装             make && make install   启动redis 服务 (加上&表示在后台运 ...