【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial—5.5 控制语句: for, while, if 语句
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 语句的更多相关文章
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial
Lecture 5 Octave教程 5.1 基本操作 Basic Operations 5.2 移动数据 Moving Data Around 5.3 计算数据 Computing on Data ...
- 【原】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)都看 ...
- 【原】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]; > ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 15—Anomaly Detection异常检测
Lecture 15 Anomaly Detection 异常检测 15.1 异常检测问题的动机 Problem Motivation 异常检测(Anomaly detection)问题是机器学习算法 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 16—Recommender Systems 推荐系统
Lecture 16 Recommender Systems 推荐系统 16.1 问题形式化 Problem Formulation 在机器学习领域,对于一些问题存在一些算法, 能试图自动地替你学习到 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 14—Dimensionality Reduction 降维
Lecture 14 Dimensionality Reduction 降维 14.1 降维的动机一:数据压缩 Data Compression 现在讨论第二种无监督学习问题:降维. 降维的一个作用是 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 13—Clustering 聚类
Lecture 13 聚类 Clustering 13.1 无监督学习简介 Unsupervised Learning Introduction 现在开始学习第一个无监督学习算法:聚类.我们的数据没 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 12—Support Vector Machines 支持向量机
Lecture 12 支持向量机 Support Vector Machines 12.1 优化目标 Optimization Objective 支持向量机(Support Vector Machi ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 11—Machine Learning System Design 机器学习系统设计
Lecture 11—Machine Learning System Design 11.1 垃圾邮件分类 本章中用一个实际例子: 垃圾邮件Spam的分类 来描述机器学习系统设计方法.首先来看两封邮件 ...
随机推荐
- 剑指offer--33.丑数
本来用数组做标志位,但是测试数据有第1500个,859963392,惹不起哦 ------------------------------------------------------------- ...
- iOS Charles Proxy 教程
原文:Charles Proxy Tutorial for iOS 作者:Aaron Douglas 译者:kmyhy 有时不得不承认--我们曾经写过一些代码,它们不正确,但我们又无法调试.更麻烦的是 ...
- Lua基础---运算符
众所周知,C,C++,python等语言都有运算符,那么Lua也不例外,因为它是C写的嘛! Lua分为主要三类运算符,分别是算术运算符,关系运算符,逻辑运算符,还有特殊运算符. 1.算术运算符有: + ...
- Flask中的ORM使用
前言 ORM拓展 安装 数据库设置 使用 关系 单表操作 建表 应用表结构 CRUD 添加查找操作 更新操作 删除操作 一对多 多对多 总结 前言 最近几天接触了一下Flask,在惊叹于其简洁性的同时 ...
- JavaScript 中 this:
在绑定事件时,this指定的是被绑定的那个html元素,比如: 结果: press里的this指的就是canvas.
- GO语言打包ICO图标
1. go get github.com/akavel/rsrc2. 创建manifest文件, 命名:main.exe.manifest : <?xml version="1.0&q ...
- PS常用美化处理方法大全
学习PS的同学都知道,我们日常生活中使用PS就是进行一些简单的图像美白,图像颜色的优化,其他的基本不用,在长时间的PS使用过程中本人总结了一些处理皮肤的方法,都是一些非常简单的方法,希望能够帮助那些刚 ...
- .NET面试题总结
1.c#垃圾回收机制 从以下方面入手展开: 1.压缩合并算法 2.代的机制 3.GC调用终结器 2.委托和事件 先说它的定义:委托的本质是类,类型安全的指针,然后从用途上考虑,事件是包装的委托 ...
- matlab的fda工具使用方法
MATLAB中用FDATool设计滤波器及使用 该文章讲述了MATLAB中用FDATool设计滤波器及使用. 1. 在Matlab中键入fdatool运行Filter Design and Analy ...
- python操作rabbitmq、redis
1.启动rabbimq.mysql 在“”运行“”里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd,进入redis所在目录,执行redis- ...