[Machine Learning] Octave Control Statements, for while if

For:

复制代码
v = zeros(10, 1);

for i=1:10,
    v(i) = 2^i;
end;

# the same as

indices=1:10
for i=indices,
    disp(i)
end;
复制代码

 

while & if & break:

i=1;
while i <=5,
    v(i) = 100;
    i = i + 1;
    if i ==6,
       break;
    end;
end;    

 

 

if & elseif:

if v(1) ==1,
    disp(The value is one')
elseif v(1) == 2,
    disp('The value is two'')
else
    disp('The value is not one or two');
end;

 

function:

For exmaple, you have a file called 'squareThisNumber.m' , inside the file, there is a function:

function y = squareThisNumber(x)

y =x ^ 2;

function can return multi values:

function [y1, y2] = squareAndCubeThisNumber(x)

y1 = x ^ 2;
y2 = x ^ 3;

Call the function get the result:

[a,b] = squareAndCubeThisNumber(5);
a # 25
b # 125

 

Example:

复制代码
X = [1 1; 1 2; 1 3];
y = [1; 2; 3];
theta = [0;1];

j = costFunction(X, y, theta)


function J = costFunctionJ(X, y, theta)

m = size(X, 1) # number of training examples
predictions = X*theta; # predictions of hypothesis on all m examples
sqrErrors = (predictions-y).^2;

J = 1/(2*m) * sum(sqrErrors);
复制代码

 

Search path:

If you want to run that file, but you local in a different directory, you need to add that directory into your search path:

addpath('/usr/xxxx/octave') # add the directory where the file locate
squreThisNumber(5)

 

posted @   Zhentiw  阅读(122)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-08-19 [Dart] final vs const
2015-08-19 [rxjs] Async, handle data over time
2015-08-19 [rxjs] Creating An Observable with RxJS
点击右上角即可分享
微信分享提示