码迷,mamicode.com
首页 > 其他好文 > 详细

第二周 第五部分

时间:2019-12-14 21:06:52      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:red   pat   后缀名   style   square   循环   更改   class   一个   

循环

 

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

 

>> X = [1 1;2 2;3 3]
X =

   1   1
   2   2
   3   3

>> Y =[1;2;3]
Y =

   1
   2
   3

>> thera = [0;1]
thera =

   0
   1
cosf.m
function J =cosf(X,Y,thera),
m =size(X,1);
predictions = X*thera;
sqrerrors = (predictions-Y).^2;%用.^表示^
J = 1/(2*m) *sum(sqrerrors);
>> J = cosf(X,Y,thera)
J = 0%完美拟合

改变thera
>> thera = [0;0];
>> J = cosf(X,Y,thera)
J =  2.3333
(1^2+2^2+3^2)/(2*3)=7/3

 

 

第二周 第五部分

标签:red   pat   后缀名   style   square   循环   更改   class   一个   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!