首页 > 其他 > 详细

第二周 第五部分

时间:2019-12-14 20:31:00      阅读:76      评论:0      收藏:0      [点我收藏+]

循环

 

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

 

现在对square.m进行更改
function [y1,y2] = square(x)%返回多个值
 y1 =x^2;
 y2 = x^3;

>> pwd
ans = E:\dasande\dasi\okafter\deep learning\material
>> [a,b] = square(5);
>> a
a =  25
>> b
b =  125

 

第二周 第五部分

原文:https://www.cnblogs.com/tingtin/p/12040656.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!