我想用朱莉娅的1D阵列制作一个矩阵

问题描述 投票:0回答:1

我是朱莉娅的新手,需要一些帮助。我有一个1D阵列列表,我想生成这样的矩阵

   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]

我能够按照文档给出的简单示例,但如何使用循环实现此目的?

干杯,哎呀

julia
1个回答
1
投票

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

带循环

horizontally:

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

vertically:

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
© www.soinside.com 2019 - 2024. All rights reserved.