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. selenium2中关于Python的常用函数

    driver = webdriver.Chrome(chromeDriver) 1.返回当前会话中的cookies:driver.get_cookies() 2.根据cookies name查找:dr ...

  2. 【python】没有root权限的时候安装Python package

    下载相关位置的包 1.首先在git上下载对应的包: 搜索package github,找到地址.使用 git clone https://xxx.git 命令 2.使用python setup.py ...

  3. Cisco DHCP 配置要点

    实验拓扑图:IOU5/6/7模拟主机 IOU1为DHCP服务器 IOU2为DHCP中继器 IOU3/4为局域网内的交换机 在IOU1中配置DHCP配置 IOU2作为DHCP中继,在E0/0.10和E0 ...

  4. Cloudera Manager (centos)安装详细介绍

    文章全部来自:Cloudera Manager (centos)安装详细介绍http://www.aboutyun.com/thread-9190-1-1.html(出处: about云开发) 这里已 ...

  5. 【spring源码学习】spring的事件发布监听机制源码解析

    [一]相关源代码类 (1)spring的事件发布监听机制的核心管理类:org.springframework.context.event.SimpleApplicationEventMulticast ...

  6. LG3380 3380 【模板】二逼平衡树(树套树)

    题意 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数值 查询k在区间内的前驱(前驱定义为严格小于x ...

  7. 如何移除双系统mac中的windows系统

    双系统 双系统即在电脑的不同分区中安装两个系统,两个系统不会互相影响,但是同时只能有一个系统正在运行,并且必须通过重启的方式来更换系统. 双系统一般由于解决对不同系统的需求,而且在电脑中直接安装系统也 ...

  8. php与JAVA的RSA加密互通

    Java 版本RSA 进行加密解密 在网上查询了好几天,最终找到解决方案,网络上都是通过Cipher.getInstance("RSA"); 而改成Cipher.getInstan ...

  9. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) C. Bear and Drawing

    题目链接:http://codeforces.com/contest/573/problem/C题目大意:在两行无限长的点列上面画n个点以及n-1条边使得构成一棵树,并且要求边都在同一平面上且除了节点 ...

  10. emqtt 4 (我要publish消息了)

    这次,分析处理publish msg的流程. 由protocol开始 publish 类型的packet的处理是: process(Packet = ?PUBLISH_PACKET(_Qos, Top ...