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. L134

    这种成功和后来的研究(表明记忆本身并不是先天决定的)使爱立信总结到,记忆的行为与其说是一种习得的行为不如说是一种先天的行为. 这点我们不清楚-构思物体和找出数字模型的能力,回答问题(最好的诗人和哲学家 ...

  2. 【linux】ulimit限制打开的文件数量

    以限制打开文件数为例. ulimit -Hn 查看硬限制. ulimit -Sn 查看软限制. ulimit -n 查看两个中更小的限制(软限制始终比硬限制低, 所以查看的是软限制) 设定规则 1.软 ...

  3. Re-install Flyme or Native Google Android on Meizu MX4 Ubuntu (by quqi99)

    作者:张华 发表于:2017-06-23 版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) ## ...

  4. mysql的5.6版本支持分区吗?

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/72291698 本文出自[我是干勾鱼的博客] 我们知道,查看mysql是否支持分区 ...

  5. 从零开始编写深度学习库(五)Eigen Tensor学习笔记2.0

    1.extract_image_patches函数的使用: 假设Eigen::Tensor形状为(3,8,8,9),现在要对第二维.第三维根据size大小为(2,2),stride=(2,2),那么如 ...

  6. [QT][转载] Qt信号和槽

    From: http://blog.csdn.net/rl529014/article/details/51346955 GUI 程序除了要绘制控件,还要响应系统和用户事件,例如重绘.绘制完成.点击鼠 ...

  7. 牛顿方法的简单MATLAB编程示意

    function y = f(x) y=(x-2)^2; function x0 syms x; x0=rand; while f(x0)~=0 x0=-f(x0)/vpa(subs(diff((x- ...

  8. 20165222《Java程序设计》——实验二 面向对象程序设计

    20165222<Java程序设计>——实验二 面向对象程序设计 提交点一.JUnit测试用例 知识点:这里就是了解测试代码的应用,测试代码的书写为:@Test assertEquals( ...

  9. 数据结构(栈&堆 )

    在计算机领域,堆栈是一个不容忽视的概念,堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈是个特殊的存储区,主要功能 ...

  10. 【转载】对一致性Hash算法,Java代码实现的深入研究

    原文地址:http://www.cnblogs.com/xrq730/p/5186728.html 一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细 ...