Octave(控制语句)

 

for循环遍历

>> v = zeros(10,1)
v =

   0
   0
   0
   0
   0
   0
   0
   0
   0
   0

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

      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024

 

或者:

>> i=1:10;
>> i
i =

    1    2    3    4    5    6    7    8    9   10

>> for v =i,
disp(v);
end;

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10

 

 

while循环:

>> v = zeros(10,1);
>> i=1;
>> while i<=5,
v(i) = 100;
i=i+1;
end;
>> v
v =

   100
   100
   100
   100
   100
     0
     0
     0
     0
     0

 

 

>> i=1;
>> while true,
     v(i) = 999;
     i=i+1;
     if i==6,
         break;
     end;
end;
>> v
v =

   999   999   999   999   999

 

 

if语句:

>> v(1) =2;
>> if v(1) ==2.
end;
>> if v(1) ==2,
       disp('is 2');
   elseif v(1) ==1,
       disp('is 1');
   else
        disp('is not 1 or 2');
end;
is 2

 

 

调用函数:

在当前目录下创建ying.m文件,点击编辑:

function y = ying(x)

y = x^2;

 

然后调用:

>> ying(2)
ans  = 4

 

posted @ 2018-11-04 00:05  我星伤悲  阅读(591)  评论(0编辑  收藏  举报