Octave: 基本操作 part .2


* 行列の要素同士の積
Octaveにおいて「.」は要素単位の演算を表す。

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

   1   2
   3   4
   5   6

>> b = 10 * ones(3, 2)
b =

   10   10
   10   10
   10   10

>> a .* b
ans =

   10   20
   30   40
   50   60

>> a * b
error: operator *: nonconformant arguments (op1 is 3x2, op2 is 3x2)
```

* 行列の要素単位の累乗

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

   1   2
   3   4
   5   6

>> a .^ 2
ans =

    1    4
    9   16
   25   36
```

* 行列の各要素への加算

```matlab

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

   1   2
   3   4
   5   6

>> a + 1
ans =

   2   3
   4   5
   6   7
```


* 行列の転置

```matlab

>> a
a =

   1   2
   3   4
   5   6

>> a'
ans =

   1   3   5
   2   4   6

>> (a')'
ans =

   1   2
   3   4
   5   6
```

* 行列の最大値とそのインデックス

列ごと、行ごとに最大値が返る。

```matlab
>> a = randi(100, 4)
a =

   56   91   27   66
   11   80   96   91
   96   41   53   62
   68   30    1   40

>>
>> max(a)
ans =

   96   91   96   91

>> max(a, [], 1)
ans =

   96   91   96   91

>> [val, index] = max(a, [], 1)
val =

   96   91   96   91

index =

   3   1   2   2

>> [val, index] = max(a, [], 2)
val =

   91
   96
   96
   68

index =

   2
   3
   1
   1

>>
```

行列全体からの最大値は以下のように取得

```matlab

>> max(max(a))
ans = 96
```

* 列の合計

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

   1   2
   3   4
   5   6

>> sum(a)
ans =

    9   12

>> sum(a, 1)
ans =

    9   12
```

* 行の合計

```matlab

>> sum(a, 2)
ans =

    3
    7
   11 
```

* n以下の要素とそのインデックス

findは行列を左上から右下に向かって数えた時のインデックスを返す。

```matlab
>> a = randi(10, 4, 4)
a =

   7   7   3   3
   6   3   5   8
   1   2   7   3
   8   4   8   5

>>
>> a < 5
ans =

  0  0  1  1
  0  1  0  0
  1  1  0  1
  0  1  0  0

>>
>> find(a < 5)
ans =

    3
    6
    7
    8
    9
   13
   15
```

* 魔法陣 (Magic Square)

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

   8   1   6
   3   5   7
   4   9   2
```

* (擬似)逆行列

```matlab
>> a = magic(3)
a =

   8   1   6
   3   5   7
   4   9   2

>> pinv(a)
ans =

   0.147222  -0.144444   0.063889
  -0.061111   0.022222   0.105556
  -0.019444   0.188889  -0.102778

>>
```

* プロット

```matlab
>> t = [0: 0.1: 2]
t =

 Columns 1 through 18:

        0   0.1000   0.2000   0.3000   0.4000   0.5000   0.6000   0.7000   0.8000   0.9000   1.0000   1.1000   1.2000   1.3000   1.4000   1.5000   1.6000   1.7000

 Columns 19 through 21:

   1.8000   1.9000   2.0000

>> y = sin(t * pi)
y =

 Columns 1 through 18:

        0   0.3090   0.5878   0.8090   0.9511   1.0000   0.9511   0.8090   0.5878   0.3090   0.0000  -0.3090  -0.5878  -0.8090  -0.9511  -1.0000  -0.9511  -0.8090

 Columns 19 through 21:

  -0.5878  -0.3090  -0.0000

>> plot(t, y)
```

* 複数のグラフのプロット、ラベル、ファイル出力

```matlab
>> plot([0: 0.1: 2], sin([0:0.1:2] * pi), 'b')
>> hold on
>> plot([0: 0.1: 2], cos([0:0.1:2] * pi), 'r')

>> xlabel('t')
>> ylabel('y')
>> legend('sin', 'cos')
>> title('test plot')

>> print -dpng plot.png
```

![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/802246/9d37514f-8dc2-7d78-de3d-8cc4252af049.png)


* 複数ウィンドウへのプロット

```matlab
>> figure(1)
>> plot(1)
>> figure(2)
>> plot(2)
```

* ウィンドウの分割

```matlab
>> subplot(3, 3, 1)
>> plot([0: 0.1: 2], cos([0:0.1:2] * pi), 'r')
>> subplot(3, 3, 5)
>> plot([0: 0.1: 2], sin([0:0.1:2] * pi), 'b')
```

![subplot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/802246/ab417724-6f17-8255-e935-9d81dced85bc.png)


* ヒートマップ

```matlab
imagesc(rand(100, 50)), colorbar
```

![heatmap.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/802246/309458e0-5b2a-7426-c0f0-f9c1f1472850.png)