Blog

懒癌晚期


Project maintained by VirusPC Hosted on GitHub Pages — Theme by mattgraham

Back Home

控制语句


for循环

可用于遍历一维向量(行向量而非列向量)。形如:for 一维向量, 语句1; 语句2; ... end;

>> v = zeros(10, 1);
>> for i=1:10,
>    v(i)=2^i;  % 缩进只是为了美观
>  end;
>> v
v =
v =

      1
      4
      8
     16
     32
     64
    128
    256
    512
   1024
      2      4      8     16     32     64    128    256    512   1024

>> indices = 1:10;
>> for i=indices,
>    disp(i);
>  end;
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10

while循环

形如: while 条件语句, 语句1; 语句2; ... end;

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

    100
    100
    100
    100
    100
     64
    128
    256
    512
   1024

if条件语句

>> 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 is not one or two.');
> end;
The value is two

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

函数


综述

多个参数和返回值

损失函数(cost function)


课程链接