octave控制语句

来源:互联网 发布:interbase数据库启动 编辑:程序博客网 时间:2024/06/11 00:23

for循环

v=zeros(10,1)      #定义v向量

for i=1:10,           #i从1到10

     v(i)=2^i;           #给v(i)赋值

end;                    #结束循环

while循环

i=1;

while i<=5,

   v(i)=100;

   i=i+1;

end;

if else

v(3)=4;

if v(3)==1,

   disp('The value is one');

else if v(3)==2,

   disp('The value is two');

else

   disp('The value is not one or two');

end;


函数

将函数保存在.m文件中,在octave中进入该文件所在位置 cd 'E:\coursera';

运行函数就输入文件名以及参数例如 squareThisNumber(5)

注意:要进入文件所在目录下运行该文件或者将路径添加到搜索路径中。

addpath('E:\coursera');


可以有多个返回值

定义一个函数SquareAndCubeThisNumber(x)可以求一个数的平方和立方

[a,b]=SquareAndCubeThisNumber(5);

结果a=25 b=125