1.使用for循环
v=zeros(10,1); %初始化一个10行1列向量
for i=1:10
v(i)=2^i;
end
indices=1:10;
for i=indices %等同于for i=1:10
disp(i);
endfor %等同于end
2.使用while
v=zeros(10,1);
for i=1:10
v(i)=2^i;
end;
i=1;
while i<=5
v(i)=100;
i=i+1;
end;
3.使用while if break
v=zeros(10,1);
for i=1:10
v(i)=2^i;
end;
i=1;
while true
v(i)=999;
i=i+1;
if i==6
break; %跳出当前循环
end;
end;
4.使用if elseif else
v=[2 4 8 16 32 64 128 256 512 1024];
v(1)=2;
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.');
endif
5.使用exit
或quit
可以退出Octave实时命令;
6.使用pwd
查看当前工作目录,使用cd '目录'
切换当前工作目录;
7.使用addpath('目录');
添加路径到Octave/Matlab系统中,无论当前目录在哪里都能访问addpath过的目录;
8.在Octave/Matlab系统,自定义函数一般放在一个函数名.m
文件中;
%文件名SqAndCuNum.m
function [y1 y2] = SqAndCuNum (x)
y1=x^2;
y2=x^3;
endfunction