【原】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的分类 来描述机器学习系统设计方法.首先来看两封邮件 ...
随机推荐
- Week2《Java程序设计》第二周学习总结
Week02-Java学习笔记2 1. 本周学习总结 本周学习了java的基本语法与类库,其中介绍了基本数据类型和引用类型,介绍了String类,String对象的拼接以及String的一些常用方法: ...
- Flask--信号 blinker
Flask--信号 blinker Flask框架中的信号基于blinker,可以让开发者在flask请求过程中 定制一些用户行为执行. 在请求前后,模板渲染前后,上下文前后,异常 的时候 安装: p ...
- ubuntu 上的ruby安装
安装 rbenv git clone git://github.com/sstephenson/rbenv.git ~/.rbenv # 用来编译安装 ruby git clone git://git ...
- SQLServer流水号自动生成
最近给客户做生成条码的功能时,碰到个问题,需要根据数量自动生成流水号,然后加上客户指定的前缀,组合成条码. 折腾了一会,最后通过个存储过程实现. --@Prefix 指定前缀,@InitialVal ...
- PS基础教程[3]如何去除照片上的水印
网络上的照片大部分都有很多的水印,要嘛就是网站的地址,要嘛就是一些煽情的文字,我们看图片想要的可不是这些东西,那么我们怎样去掉图片上的水印呢?本次我们就来分享一下仿制图章工具的使用. 方法 1.打开P ...
- Oracle Sql Developer 连接 SqlServer
1.下载 jTDS - SQL Server and Sybase JDBC driver 地址:http://sourceforge.net/projects/jtds/files/ 2.运行SQL ...
- WPF 自定义DateControl DateTime控件(转)
自定义日期控件,月份选择.如下是日期的一些效果图. 具体的样式.颜色可以根据下面的代码,自己调节即可 1.日期控件的界面 <UserControl x:Class="WpfApp ...
- Python学习流程
这是我在过去几家公司招聘到工程师,Python入职培训的过程. 时间分为4周,全部自学,仅提供大纲.适用于Web方向: 1.Week1:读完<简明Python教程>,适应Python开发环 ...
- nginx time_wait 较多优化
1. 查看命令 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 结果 ESTABLISHED 22 F ...
- MDK中STM32使用Printf函数详细解析【转载】
在用MDK调试STM32板子串口时,为了方便串口调试,调用了printf()函数,用Keil仿真是,串口不能正确的输出,软件仿真时,总是卡在那 里.有点纳闷,然后调用USART_SendData()函 ...