Octave教程(五)
来源:互联网 发布:cst仿真软件价格 编辑:程序博客网 时间:2024/06/02 00:58
控制语句
V = 0 0 0 0 0 0 0 0 0 0>> for i=1:10, % for循环> V(i) = 2 ^ i;> end;>> VV = 2 4 8 16 32 64 128 256 512 1024>> indices = 1:10;>> for i=indices, % for循环遍历输出> disp(i);> end; 1 2 3 4 5 6 7 8 9 10>> i = 1;>> while i <= 5, % while循环> V(i) = 100;> i = i + 1;> end;>> VV = 100 100 100 100 100 64 128 256 512 1024>> i = 1;>> while true,> V(i) = 999;> i = i + 1;> if i == 6,> break; % break与continue的使用> end;> end;>> VV = 999 999 999 999 999 64 128 256 512 1024>> V(1) = 2;>> if V(1) == 1, % if-else语句> disp('The value is one');> elseif V(1) == 2,> disp('The value is two');> else> disp('The value is not one or two');> end;The value is two
函数
我们先创建costFunctionJ.m文件,并在其添加如下代码:
function J = costFunctionJ(X, y, theta)% X is the "design matrix" contanining our tranining examples.% y is the class labels.m = size(X, 1); % number of tranining examplespredictions = X*theta; % predictions of hypothesis on all m examplessqrErrors = (predictions-y).^2; % squared sqrErrorsJ = 1/(2*m) * sum(sqrErrors);
然后,我们在Octave的CLI中键入如下命令:
% 先利用cd命令前往costFunctionJ.m文件目录路径>> X = [1 1; 1 2; 1 3]X = 1 1 1 2 1 3>> y = [1; 2; 3]y = 1 2 3>> theta = [0;1];>> j = costFunctionJ(X, y, theta)j = 0
注:文件名需与函数名一致。
阅读全文
0 0
- Octave教程(五)
- Octave教程(一)
- Octave教程(二)
- Octave教程(三)
- Octave教程(四)
- Octave教程(六)
- Octave教程 Octave Tutorial
- 斯坦福机器学习笔记 第2周 五、Octave教程(一)
- 斯坦福机器学习笔记 第2周 五、Octave教程(二)
- 机器学习笔记(五) octave入门
- Octave安装教程
- Octave安装教程
- 吴恩达 机器学习笔记五(lecture 5)(Octave 使用)
- Octave教程五:控制语句if/while/for以及函数的定义和使用
- Octave教程一:基本操作
- Octave教程四:Plotting Data
- Octave
- Octave
- 56G 线缆在数据中心的应用
- Octave教程(二)
- Octave教程(三)
- Octave教程(四)
- 使用.net core ABP和Angular模板构建博客管理系统(实现自己的业务逻辑)
- Octave教程(五)
- Octave教程(六)
- 编程作业(一)
- 逻辑回归(一)
- 【中国大学MOOC-陈越、何钦铭-数据结构-2017秋】最大子列和问题
- 逻辑回归(二)
- 逻辑回归(三)
- 正则化
- 编程作业(二)