# 矩阵和数组

MATLAB 是“matrix laboratory”的缩写形式。MATLAB® 主要用于处理整个的矩阵和数组，而其他编程语言大多逐个处理数值。

### 数组创建

`a = [1 2 3 4]`
```a = 1×4 1 2 3 4 ```

`a = [1 3 5; 2 4 6; 7 8 10]`
```a = 3×3 1 3 5 2 4 6 7 8 10 ```

`z = zeros(5,1)`
```z = 5×1 0 0 0 0 0 ```

### 矩阵和数组运算

MATLAB 允许您使用单一的算术运算符或函数来处理矩阵中的所有值。

`a + 10`
```ans = 3×3 11 13 15 12 14 16 17 18 20 ```
`sin(a)`
```ans = 3×3 0.8415 0.1411 -0.9589 0.9093 -0.7568 -0.2794 0.6570 0.9894 -0.5440 ```

`a'`
```ans = 3×3 1 2 7 3 4 8 5 6 10 ```

`p = a*inv(a)`
```p = 3×3 1.0000 0 0 0 1.0000 0 0 -0.0000 1.0000 ```

```format long p = a*inv(a)```
```p = 3×3 0.999999999999996 0 0 0 1.000000000000000 0 0 -0.000000000000014 1.000000000000000 ```

`format short`

`format` 仅影响数字显示，而不影响 MATLAB 对数字的计算或保存方式。

`p = a.*a`
```p = 3×3 1 9 25 4 16 36 49 64 100 ```

`a.^3`
```ans = 3×3 1 27 125 8 64 216 343 512 1000 ```

### 串联

`A = [a,a]`
```A = 3×6 1 3 5 1 3 5 2 4 6 2 4 6 7 8 10 7 8 10 ```

`A = [a; a]`
```A = 6×3 1 3 5 2 4 6 7 8 10 1 3 5 2 4 6 7 8 10 ```

### 复数

`sqrt(-1)`
```ans = 0.0000 + 1.0000i ```

`c = [3+4i, 4+3j; -i, 10j]`
```c = 2×2 complex 3.0000 + 4.0000i 4.0000 + 3.0000i 0.0000 - 1.0000i 0.0000 +10.0000i ```