CourseraのMachine Learningのweek2に出てくるOctaveの文法まとめ


はじめに

最近人気の CourseraのMachine Learning を勉強し始めました。
この講座の中ではOctaveかMATLABを使うのですが、初めて使う言語なので勉強のために文法をメモしてみます。
僕はMATLABを使用することにしました。

Basic Operations

% はコメント
% 加減乗除: +, -, *, /

>> 2^6 % 累乗
ans =
    64

>> 1 == 2 % false
ans =
  logical
   0

>> 1 ~= 2 % true
ans =
  logical
   1

>> 1 && 0
ans =
  logical
   0

>> 1 || 0
ans =
  logical
   1

>> xor(1,0)
ans =
  logical
   1

>> a = 3; % セミコロンをつけると出力しない

>> b = 'hi';

>> a = pi;
>> disp(a);
    3.1416

>> disp(sprintf('2 decimals: %0.2f', a))
2 decimals: 3.14

>> format long
>> disp(a);
   3.141592653589793

>> format short
>> disp(a);
    3.1416

>> A = [1 2; 3 4; 5 6]
A =
     1     2
     3     4
     5     6

>> A = [1 2;
3 4;
5 6] % このような書き方もできる

A =
     1     2
     3     4
     5     6

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

>> v = 1:0.1:2
v =
    1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000    1.8000    1.9000    2.0000

>> v = 1:6
v =
     1     2     3     4     5     6

>> ones(2,3)
ans =
     1     1     1
     1     1     1


>> C = 2*ones(2,3)
C =
     2     2     2
     2     2     2

>> w = ones(1, 3)
w =
     1     1     1

>> w = zeros(1, 3)
w =
     0     0     0

>> w = rand(3, 3)
w =
    0.9040    0.2420    0.8128
    0.9409    0.9757    0.6974
    0.8025    0.3172    0.2695

>> w = randn(1, 3)
w =
    0.5371   -0.3804   -1.5310

>> w = -6 + sqrt(10)*(randn(1,10000));

>> hist(w) % histgramを表示
>> hist(w, 50)

>> I = eye(4)
I =
     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1

Moving Data Around

>> disp(A)
     1     2
     3     4
     5     6

>> sz = size(A)
sz =
     3     2

>> size(sz)
ans =
     1     2

>> size(A,1)
ans =
     3

>> size(A,2)
ans =
     2

>> a = [1 2 3 4]
a =
     1     2     3     4

>> length(a)
ans =
     4

>> length(A)
ans =
     3

>> length([1; 2; 3; 4; 5])
ans =
     5

>> load featuresX.dat % ファイルの読み込み

>> who
Your variables are:
A    a    ans  sz   

>> whos
  Name      Size            Bytes  Class     Attributes
  A         3x2                48  double              
  a         1x4                32  double              
  ans       1x1                 8  double              
  sz        1x2                16  double              

>> clear
>> who

>>  A = [1 2; 3 4; 5 6]
A =
     1     2
     3     4
     5     6

>> save hello.mat A -ascii % save as text (ASCII)

>> A(3, 2)
ans =
     6

>> A(2, :) % ":" means every element along that row/column
ans =
     3     4

>> A(:, 2)
ans =
     2
     4
     6

>> A([1 3], :)
ans =
     1     2
     5     6

>> A(:, 2) = [10; 11; 12]
A =
     1    10
     3    11
     5    12

>> A = [A, [100; 101; 102]]
A =
     1    10   100
     3    11   101
     5    12   102

>> A(:)
ans =
     1
     3
     5
    10
    11
    12
   100
   101
   102

>> B = [1 2; 3 4; 5 6]
B =
     1     2
     3     4
     5     6

>> C = [11 12; 13 14; 15 16]
C =
    11    12
    13    14
    15    16

>> D = [B C]
D =
     1     2    11    12
     3     4    13    14
     5     6    15    16

>> E = [B; C]
E =
     1     2
     3     4
     5     6
    11    12
    13    14
    15    16

>> size(E)
ans =
     6     2

Computing on Data

>> disp(A)
     1     2
     3     4
     5     6

>> disp(B)
    11    12
    13    14
    15    16

>> disp(C)
     1     1
     2     2

>> A * C
ans =
     5     5
    11    11
    17    17

>> A .* B
ans =
    11    24
    39    56
    75    96

>> A .^ 2
ans =
     1     4
     9    16
    25    36

>> v
v =
     1
     2
     3

>> 1 ./ v
ans =
    1.0000
    0.5000
    0.3333

>> log(v)
ans =
         0
    0.6931
    1.0986

>> exp(v)
ans =
    2.7183
    7.3891
   20.0855

>> abs([-1; -2; -3])
ans =
     1
     2
     3

>> v + ones(length(v), 1)
ans =
     2
     3
     4

>> v + 1
ans =
     2
     3
     4

>> A'
ans =
     1     3     5
     2     4     6

>> (A')'
ans =
     1     2
     3     4
     5     6


>> max(A)
ans =
     5     6

>> a = [1 15 2 0.5]
a =
    1.0000   15.0000    2.0000    0.5000

>> [val, ind] = max(a)
val =
    15
ind =
     2

>> a < 3
ans =
  1×4 logical array
   1   0   1   1

>> find(a < 3)
ans =
     1     3     4

>> A = magic(3)
A =
     8     1     6
     3     5     7
     4     9     2

>> [row, column] = find(A >= 7)
row =
     1
     3
     2
column =
     1
     2
     3

>> a
a =
    1.0000   15.0000    2.0000    0.5000

>> sum(a)
ans =
   18.5000

>> prod(a) % 掛ける
ans =
    15

>> floor(a)
ans =
     1    15     2     0

>> ceil(a)
ans =
     1    15     2     1

>> max(rand(3), rand(3))
ans =
    0.9040    0.7098    0.8128
    0.9409    0.9757    0.7385
    0.8025    0.3172    0.9410

>> A
A =
     8     1     6
     3     5     7
     4     9     2

>> max(A)
ans =
     8     9     7

>> max(A,[],1)
ans =
     8     9     7

>> max(A,[],2)
ans =
     8
     7
     9

>> max(max(A))
ans =
     9

>> max(A(:))
ans =
     9

>> A = magic(9)
A =
    47    58    69    80     1    12    23    34    45
    57    68    79     9    11    22    33    44    46
    67    78     8    10    21    32    43    54    56
    77     7    18    20    31    42    53    55    66
     6    17    19    30    41    52    63    65    76
    16    27    29    40    51    62    64    75     5
    26    28    39    50    61    72    74     4    15
    36    38    49    60    71    73     3    14    25
    37    48    59    70    81     2    13    24    35

>> sum(A,1)
ans =
   369   369   369   369   369   369   369   369   369

>> sum(A,2)
ans =
   369
   369
   369
   369
   369
   369
   369
   369
   369

>> eye(9)
ans =
     1     0     0     0     0     0     0     0     0
     0     1     0     0     0     0     0     0     0
     0     0     1     0     0     0     0     0     0
     0     0     0     1     0     0     0     0     0
     0     0     0     0     1     0     0     0     0
     0     0     0     0     0     1     0     0     0
     0     0     0     0     0     0     1     0     0
     0     0     0     0     0     0     0     1     0
     0     0     0     0     0     0     0     0     1

>> A .* eye(9)
ans =
    47     0     0     0     0     0     0     0     0
     0    68     0     0     0     0     0     0     0
     0     0     8     0     0     0     0     0     0
     0     0     0    20     0     0     0     0     0
     0     0     0     0    41     0     0     0     0
     0     0     0     0     0    62     0     0     0
     0     0     0     0     0     0    74     0     0
     0     0     0     0     0     0     0    14     0
     0     0     0     0     0     0     0     0    35

>> sum(A.*eye(9))
ans =
    47    68     8    20    41    62    74    14    35

>> sum(sum(A.*eye(9)))
ans =
   369

>> flipud(eye(9))
ans =
     0     0     0     0     0     0     0     0     1
     0     0     0     0     0     0     0     1     0
     0     0     0     0     0     0     1     0     0
     0     0     0     0     0     1     0     0     0
     0     0     0     0     1     0     0     0     0
     0     0     0     1     0     0     0     0     0
     0     0     1     0     0     0     0     0     0
     0     1     0     0     0     0     0     0     0
     1     0     0     0     0     0     0     0     0

>> sum(sum(A.*flipud(eye(9))))
ans =
   369

>> A = magic(3)
A =
     8     1     6
     3     5     7
     4     9     2

>> temp = pinv(A)
temp =
    0.1472   -0.1444    0.0639
   -0.0611    0.0222    0.1056
   -0.0194    0.1889   -0.1028

>> temp * A
ans =
    1.0000    0.0000   -0.0000
   -0.0000    1.0000    0.0000
    0.0000   -0.0000    1.0000

Plotting Data

>> t = [0:0.01:0.98];
>> y1 = sin(2*pi*4*t);
>> plot(t,y1);
>> y2 = cos(2*pi*4*t);
>> plot(t,y2);
>> hold on; % 複数のプロットを同じ図に
>> plot(t,y1,'r');
>> xlabel('time');
>> ylabel('value');
>> legend('cos', 'sin');
>> title('my plot');
>> print -dpng 'myPlot.png'
>> close % 閉じる

>> figure(1); plot(t, y1); % 複数開く
>> figure(2); plot(t, y2);
>> subplot(1,2,1); % Divides plot a 1×2 grid. access first element.
>> plot(t, y1);
>> subplot(1,2,2);
>> plot(t, y2);
>> axis([0.5 1 -1 1])
>> clf;

>> A = magic(5)
A =
    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

>> imagesc(A)
>> imagesc(magic(10)), colorbar, colormap gray;

Control Statements: for, while, if statement

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

>> v
v =
           2
           4
           8
          16
          32
          64
         128
         256
         512
        1024

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

>> v
v =
           2
           4
           8
          16
          32
          64
         128
         256
         512
        1024

>> i = 1
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

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