循环
>> v = zeros(10,1)
v =
0
0
0
0
0
0
0
0
0
0
>> for i=1:10,
> v(i) = 2^i;
> end;
>> v
v =
2
4
8
16
32
64
128
256
512
1024
>> indices=1:10; >> indices indices = 1 2 3 4 5 6 7 8 9 10 >> for i =indices, > disp(i);%空格不重要 > end; 1 2 3 4 5 6 7 8 9 10
while >> i=1; >> while i<=5, > v(i)=100; > i=i+1; > end; >> v v = 100 100 100 100 100 64 128 256 512 1024
>> i=1; >> while true, > v(i)=999; > i=i+1; > if i==6, > break; > end;%要有end > end; >> v v = 999 999 999 999 999 64 128 256 512 1024
>> v(1) ans = 999 >> 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 error'); > end; The value is two
exit 或者 quit 可以退出Octave
调用函数: 创建一个文件,以你的函数来命名(后缀名 .m)。进入文件的目录再调用函数才可以 如:square.m function y = square(x) y =x^2;
>> pwd ans = E:\dasande\dasi\okafter\deep learning\material >> square(3) ans = 9
在C:\Users\cltt\Desktop保存一个文件f.m function y =f(x), y = x+3; >> addpath('C:\Users\cltt\Desktop')%添加Octave 的寻找路径 >> pwd ans = E:\dasande\dasi\okafter\deep learning\material >> f(2)%即使不再f.m的目录下,此时依然可以调用函数f ans = 5