Julia语言中矩阵常用操作

1.1 矩阵的生成

生成一个4行4列的矩阵, 这里使用1~16数字. 注意, 这里生成矩阵时, 需要首先定义一个空的数组, 然后再进行填充.

mat = Array(Int32,4,4)
4×4 Array{Int32,2}:
 125804192  256236432   79364176   79364176
         0          0          0          0
  79234864   79266064  125805712  248775184
         0          0          0          0
mat[:]=1:16
mat
4×4 Array{Int32,2}:
 1  5   9  13
 2  6  10  14
 3  7  11  15
 4  8  12  16
也可以使用reshape构建矩阵
reshape(1:15,3,5)
3×5 Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}:
 1  4  7  10  13
 2  5  8  11  14
 3  6  9  12  15

1.2 提取主对角线

diag(mat)
4-element Array{Int64,1}:
  1
  6
 11
 16

1.3 生成对角线为1的对角矩阵

eye(4)
4×4 Array{Float64,2}:
 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

1.4 提取矩阵的下三角

tril(mat)
4×4 Array{Int64,2}:
 1  0   0   0
 2  6   0   0
 3  7  11   0
 4  8  12  16

1.5 提取矩阵的上三角

triu(mat)
4×4 Array{Int64,2}:
 1  5   9  13
 0  6  10  14
 0  0  11  15
 0  0   0  16

1.6 矩阵转置

mat'
4×4 Array{Int64,2}:
  1   2   3   4
  5   6   7   8
  9  10  11  12
 13  14  15  16

1.7 矩阵相乘

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

2×2 Array{Int64,2}:
 1  2
 3  4
b = [[2,4] [3,5]]
2×2 Array{Int64,2}:
 2  3
 4  5

对应数值相乘

a.*b
2×2 Array{Int64,2}:
  2   6
 12  20

矩阵相乘

a*b
2×2 Array{Int64,2}:
 10  13
 22  29

1.8 矩阵求逆

inv(a)
2×2 Array{Float64,2}:
 -2.0   1.0
  1.5  -0.5
inv(a)*a
2×2 Array{Float64,2}:
 1.0          0.0
 2.22045e-16  1.0

猜你喜欢

转载自blog.csdn.net/yijiaobani/article/details/82785116