Octave: 基本操作 part. 1

* 四則演算

```matlab
octave:9> 1 + 1
ans = 2
octave:10> 1 - 1
ans = 0
octave:11> 1 * 2
ans = 2
octave:12> 1 / 2
ans = 0.5000
octave:13> 2 ^ 10
ans = 1024
```

* 論理演算

```matlab
octave:14> 1 && 0
ans = 0
octave:15> 1 || 0
ans = 1
octave:16> and(1, 0)
ans = 0
octave:17> or(1, 0)
ans = 1
octave:18> xor(1, 1)
ans = 0
```

* プロンプトの変更

```matlab
octave:21> PS1('>> ')
>>
>>
```

* プロンプトの変更を永続化

```matlab
$ cat ~/.octaverc
PS1(">> ")
```

* 対話モードでのセミコロン

セミコロンをつけると代入時に結果が出力されない。

```matlab
>> a = 1
a = 1
>> a = 1;
>>
```

* sprintf

```matlab
>> sprintf('%0.2f', pi)
ans = 3.14
>>
```

* format

```matlab
>> format long
>> pi
ans = 3.141592653589793
>> format short
>> pi
ans = 3.1416
>>
```

* 行列, ベクトル

```matlab
>> a = [1 2; 3 4; 5 6]
a =

   1   2
   3   4
   5   6
>>
>> a = [1 2; 3]
error: vertical dimensions mismatch (1x2 vs 1x1)
>> a = [1 2 3]
a =

   1   2   3

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

   1
   2
   3

>> 0 : 1 : 10
ans =

    0    1    2    3    4    5    6    7    8    9   10

>>
```

* 要素が全て1の行列

```matlab
>> ones(5)
ans =

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

   1   1
   1   1
   1   1
   1   1
   1   1
```

* ゼロ行列

```matlab
>> zeros(3)
ans =

   0   0   0
   0   0   0
   0   0   0

>> zeros(3, 1)
ans =

   0
   0
   0

>>
```

* 乱数行列

```matlab
>> rand(3)
ans =

   0.344418   0.673568   0.068721
   0.576266   0.698308   0.464631
   0.052790   0.634097   0.829997

>> rand(3 ,1)
ans =

   0.6622
   0.8481
   0.6899
```

* 正規分布のヒストグラム

```matlab
>> hist(randn(10000, 1), 100)
```

* 単位行列

```matlab
>> eye(3)
ans =

Diagonal Matrix

   1   0   0
   0   1   0
   0   0   1

>>
```

* help

```matlab
>> help

  For help with individual commands and functions type

    help NAME

  (replace NAME with the name of the command or function you would
  like to learn more about; for an operator, enclose "NAME" in quotes).

  For a more detailed introduction to GNU Octave, consult the manual.
  The manual may be read from the prompt by typing

    doc
...

```

* 行列のサイズ

```matlab
>> a = [1 2 3 ; 4 5 6]
a =

   1   2   3
   4   5   6

>> size(a)
ans =

   2   3
>>
>> size(a, 1)
ans = 2
>> size(a, 2)
ans = 3
>> length(a)
ans = 3
```

* 変数の一覧取得

```matlab
octave:2> who
Variables visible from the current scope:

ans

octave:3> a = 1
a = 1
octave:4> who
Variables visible from the current scope:

a    ans

octave:5>
```

* ファイル読み込み

```matlab
$ cat test.tsv
1   100
2   200
3   300

octave:10> load("test.tsv")
octave:11> who
Variables visible from the current scope:

a     ans   test

octave:12> test
test =

     1   100
     2   200
     3   300

octave:13>
```

* 変数の詳細

```matlab
octave:13> whos
Variables visible from the current scope:

variables in scope: top scope

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  =====
        a           1x1                          8  double
        ans         1x62                        62  char
        test        3x2                         48  double

Total is 69 elements using 118 bytes
```

* ファイル保存

```matlab
octave:26> a = [1 2; 3 4; 5 6]
a =

   1   2
   3   4
   5   6
octave:30> save aaa a

$ cat aaa
# Created by Octave 6.0.90, Sat Jan 23 10:30:13 2021 JST 
# name: a
# type: matrix
# rows: 3
# columns: 2
 1 2
 3 4
 5 6
```

* 変数の初期化

```matlab
>> a = 1
a = 1
>> who
Variables visible from the current scope:

a    ans

>> clear
>> who
>>
```

* 行列の要素取得

```matlab
>> a = [100 200; 300 400; 500 600]
a =

   100   200
   300   400
   500   600

>> a(2, 1)
ans = 300
>> a(3, 2)
ans = 600
```

* 行列からベクトルの取り出し

```matlab
>> a = [100 200; 300 400; 500 600]
a =

   100   200
   300   400
   500   600

>> a(1, :)
ans =

   100   200

>> a(:, 2)
ans =

   200
   400
   600

>>
```

* 行の追加、列の追加、代入

```matlab
>> a = [100 200; 300 400; 500 600]
a =

   100   200
   300   400
   500   600

>> [a, [1; 2; 3]]
ans =

   100   200     1
   300   400     2
   500   600     3

>> [a; [1 2]]
ans =

   100   200
   300   400
   500   600
     1     2

>> a(1, 2) = 0
a =

   100     0
   300   400
   500   600

>> a(:, 2) = [1; 2; 3]
a =

   100     1
   300     2
   500     3
```

* 行列を単一のベクトルに変換

```matlab
>> a = [100 200; 300 400; 500 600]
a =

   100   200
   300   400
   500   600

>> a(:)
ans =

   100
   300
   500
   200
   400
   600

>>
```