# NumPy - 矩阵库

NumPy 包包含一个 Matrix库`numpy.matlib`。此模块的函数返回矩阵而不是返回`ndarray`对象。

## `matlib.empty()`

`matlib.empty()`函数返回一个新的矩阵，而不初始化元素。 该函数接受以下参数。

``````numpy.matlib.empty(shape, dtype, order)
``````

1. `shape` 定义新矩阵形状的整数或整数元组
2. `Dtype` 可选，输出的数据类型
3. `order` `C` 或者 `F`

### 示例

``````import numpy.matlib
import numpy as np
print np.matlib.empty((2,2))
# 填充为随机数据
``````

``````[[ 2.12199579e-314,   4.24399158e-314]
[ 4.24399158e-314,   2.12199579e-314]]
``````

## `numpy.matlib.zeros()`

``````import numpy.matlib
import numpy as np
print np.matlib.zeros((2,2))
``````

``````[[ 0.  0.]
[ 0.  0.]])
``````

## `numpy.matlib.ones()`

``````import numpy.matlib
import numpy as np
print np.matlib.ones((2,2))
``````

``````[[ 1.  1.]
[ 1.  1.]]
``````

## `numpy.matlib.eye()`

``````numpy.matlib.eye(n, M,k, dtype)
``````

1. `n` 返回矩阵的行数
2. `M` 返回矩阵的列数，默认为`n`
3. `k` 对角线的索引
4. `dtype` 输出的数据类型

### 示例

``````import numpy.matlib
import numpy as np
print np.matlib.eye(n =  3, M =  4, k =  0, dtype =  float)
``````

``````[[ 1.  0.  0.  0.]
[ 0.  1.  0.  0.]
[ 0.  0.  1.  0.]])
``````

## `numpy.matlib.identity()`

`numpy.matlib.identity()`函数返回给定大小的单位矩阵。单位矩阵是主对角线元素都为 1 的方阵。

``````import numpy.matlib
import numpy as np
print np.matlib.identity(5, dtype =  float)
``````

``````[[ 1.  0.  0.  0.  0.]
[ 0.  1.  0.  0.  0.]
[ 0.  0.  1.  0.  0.]
[ 0.  0.  0.  1.  0.]
[ 0.  0.  0.  0.  1.]]
``````

## `numpy.matlib.rand()`

·numpy.matlib.rand()`函数返回给定大小的填充随机值的矩阵。

### 示例

``````import numpy.matlib
import numpy as np
print np.matlib.rand(3,3)
``````

``````[[ 0.82674464  0.57206837  0.15497519]
[ 0.33857374  0.35742401  0.90895076]
[ 0.03968467  0.13962089  0.39665201]]
``````

### 示例

``````import numpy.matlib
import numpy as np

i = np.matrix('1,2;3,4')
print i
``````

``````[[1  2]
[3  4]]
``````

### 示例

``````import numpy.matlib
import numpy as np

j = np.asarray(i)
print j
``````

``````[[1  2]
[3  4]]
``````

### 示例

``````import numpy.matlib
import numpy as np

k = np.asmatrix (j)
print k
``````

``````[[1  2]
[3  4]]
``````