octave控制语句

for循环

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

      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024
>> indics = 1:10;
>> for i = indics,
disp(i);
end;
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10

while循环

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

     10
     10
     10
     10
     32
     64
    128
    256
    512
   1024

break

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

    999
    999
    999
    999
    999
     64
    128
    256
    512
   1024

if 语句

>> 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;
The value is two

函数定义 

创建文件以      .m     结尾

function y = squareThisNumber(x)  % y是返回值

y = x^2;
>> squareThisNumber(5)
ans =  25

添加搜索路径,让即使octave不在需要的路径下,也可以搜索到需要的文件

>> addpath('路径')

函数返回多个值

函数定义

扫描二维码关注公众号,回复: 7047082 查看本文章

 function [y1, y2] = squareAndCubeThisNumber(x)

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

使用

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

猜你喜欢

转载自www.cnblogs.com/19990219073x/p/11366872.html