我想从Julia中的1D数组制作矩阵

时间:2018-11-28 05:18:46

标签: julia

我是Julia的新手,需要帮助。 我有一个一维数组的列表,我想生成一个像这样的矩阵

   g = [ 2.0 -1.0  0.0  0.0; 
        -1.0  2.0 -1.0  0.0;
         0.0 -1.0  2.0 -1.0;
         0.0  0.0 -1.0  3.0]

我能够遵循docs上给出的简单示例,但是如何使用循环来实现呢?

干杯, 阿育什

1 个答案:

答案 0 :(得分:1)

使用循环,您可以创建一个矩阵,然后填充它。

带有循环

水平:

julia> arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0],[1.,1.,1.,1.]]
5-element Array{Array{Float64,1},1}:
 [2.0, -1.0, 0.0, 0.0] 
 [-1.0, 2.0, -1.0, 0.0]
 [0.0, -1.0, 2.0, -1.0]
 [0.0, 0.0, -1.0, 3.0] 
 [1.0, 1.0, 1.0, 1.0]  

julia> function make_matrix(input::Vector{<:Vector})
              element_type = eltype(eltype(input))
              if (length(input) == 0)
                  return Array{element_type,2}(undef,0,0)
              end

              height,width = length(input[1]), length(input)

              for col in input
                (height == length(col)) ? nothing : throw("inconsistent array size")
              end

              output = Array{element_type}(undef,height,width)

              for i in 1:width
                output[:,i] = input[i]
              end

              return output
          end
make_matrix (generic function with 1 method)

julia> make_matrix(arr)
4×5 Array{Float64,2}:
  2.0  -1.0   0.0   0.0  1.0
 -1.0   2.0  -1.0   0.0  1.0
  0.0  -1.0   2.0  -1.0  1.0
  0.0   0.0  -1.0   3.0  1.0

垂直:

julia> function vmake_matrix(input::Vector{<:Vector})
                 element_type = eltype(eltype(input))
                 if (length(input) == 0)
                     return Array{element_type,2}(undef,0,0)
                 end

                 height,width = length(input),length(input[1])

                 for col in input
                   (width == length(col)) ? nothing : throw("inconsistent array size")
                 end

                 output = Array{element_type}(undef,height,width)

                 for i in 1:height
                   output[i,:] = input[i]
                 end

                 return output
             end
vmake_matrix (generic function with 1 method)

julia> vmake_matrix(arr)
5×4 Array{Float64,2}:
  2.0  -1.0   0.0   0.0
 -1.0   2.0  -1.0   0.0
  0.0  -1.0   2.0  -1.0
  0.0   0.0  -1.0   3.0
  1.0   1.0   1.0   1.0

无循环

没有循环,您可以使用vcathcat,具体取决于您要在其中连接数组的方向。

julia> H_arr = [ [ 2.0 -1.0  0.0  0.0],[-1.0  2.0 -1.0  0.0],[0.0 -1.0  2.0 -1.0],[0.0  0.0 -1.0  3.0] ]
4-element Array{Array{Float64,2},1}:
 [2.0 -1.0 0.0 0.0] 
 [-1.0 2.0 -1.0 0.0]
 [0.0 -1.0 2.0 -1.0]
 [0.0 0.0 -1.0 3.0] 

julia> vcat(H_arr...)
4×4 Array{Float64,2}:
  2.0  -1.0   0.0   0.0
 -1.0   2.0  -1.0   0.0
  0.0  -1.0   2.0  -1.0
  0.0   0.0  -1.0   3.0

julia> V_arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0]]
4-element Array{Array{Float64,1},1}:
 [2.0, -1.0, 0.0, 0.0] 
 [-1.0, 2.0, -1.0, 0.0]
 [0.0, -1.0, 2.0, -1.0]
 [0.0, 0.0, -1.0, 3.0] 

julia> hcat(V_arr...)
4×4 Array{Float64,2}:
  2.0  -1.0   0.0   0.0
 -1.0   2.0  -1.0   0.0
  0.0  -1.0   2.0  -1.0
  0.0   0.0  -1.0   3.0