octave control statements

for loop

>> 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 loop

>> 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

The if statement

>> 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

 

 

Function definition 

Create a file ending with .m

Y = function squareThisNumber (X) is the return value Y% 

Y = X ^ 2 ;
>> squareThisNumber(5)
ans =  25

Next add the search path, so that even if the path is not required octave, you can also search for the desired file

The addpath >> ( ' path')

A plurality of function return value

Function definition

 function [y1, y2] = squareAndCubeThisNumber(x)

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

use

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

 

Guess you like

Origin www.cnblogs.com/19990219073x/p/11366872.html