Home > other >  In Julia, how does one initialize a two-dimensional array (a matrix) in which each element is a colu
In Julia, how does one initialize a two-dimensional array (a matrix) in which each element is a colu

Time:07-06


Using Julia version 1.7.3, my understanding is that the following initializes matrix to be a 9 × 9 matrix each of whose elements is undefined …

matrix = Array{Char}( undef, 9, 9 )

println( matrix )
display( matrix )
println( )
println( typeof(matrix) )
println( size(matrix) )

Also, I understand that the expression [ c for c = '1' : '9' ] evaluates to a column vector of 9 characters (type Vector{Char}).


How does one combine the above to create a 9 × 9 matrix each of whose 81 elements is the column vector [ '1'; '2'; '3'; '4'; '5'; '6'; '7'; '8'; '9' ] ? (My expectation is that each column vector element will be modified in the program I plan to write.)   My best guess so far is …

matrix = Array{Vector{Char}}( [ c for c = '1' : '9' ], 9, 9 )

Attempting to do so results in …

ERROR: LoadError: MethodError: no method matching (Array{Vector{Char}})(::Vector{Char}, ::Int64, ::Int64)

CodePudding user response:


julia> [[ c for c = '1':'9' ] for _ in 1:9, _ in 1:9]
9×9 Matrix{Vector{Char}}:
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']  …  ['1', '2', '3', '4', '5', '6', '7', '8', '9']
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']     ['1', '2', '3', '4', '5', '6', '7', '8', '9']
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']     ['1', '2', '3', '4', '5', '6', '7', '8', '9']
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']     ['1', '2', '3', '4', '5', '6', '7', '8', '9']
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']     ['1', '2', '3', '4', '5', '6', '7', '8', '9']
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']  …  ['1', '2', '3', '4', '5', '6', '7', '8', '9']
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']     ['1', '2', '3', '4', '5', '6', '7', '8', '9']
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']     ['1', '2', '3', '4', '5', '6', '7', '8', '9']
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']     ['1', '2', '3', '4', '5', '6', '7', '8', '9']

Note that Arrays can be multidimensional (beyond 2), so a 3-dimensional array is also an option. Only requires a small change too: [ c for c = '1' : '9', _ in 1:9, _ in 1:9]

CodePudding user response:

Is this what you want?

julia> repeat('1':'9', inner=(1, 9))
9×9 Matrix{Char}:
 '1'  '1'  '1'  '1'  '1'  '1'  '1'  '1'  '1'
 '2'  '2'  '2'  '2'  '2'  '2'  '2'  '2'  '2'
 '3'  '3'  '3'  '3'  '3'  '3'  '3'  '3'  '3'
 '4'  '4'  '4'  '4'  '4'  '4'  '4'  '4'  '4'
 '5'  '5'  '5'  '5'  '5'  '5'  '5'  '5'  '5'
 '6'  '6'  '6'  '6'  '6'  '6'  '6'  '6'  '6'
 '7'  '7'  '7'  '7'  '7'  '7'  '7'  '7'  '7'
 '8'  '8'  '8'  '8'  '8'  '8'  '8'  '8'  '8'
 '9'  '9'  '9'  '9'  '9'  '9'  '9'  '9'  '9'

or

julia> [c for c in '1':'9', _ in 1:9]
9×9 Matrix{Char}:
 '1'  '1'  '1'  '1'  '1'  '1'  '1'  '1'  '1'
 '2'  '2'  '2'  '2'  '2'  '2'  '2'  '2'  '2'
 '3'  '3'  '3'  '3'  '3'  '3'  '3'  '3'  '3'
 '4'  '4'  '4'  '4'  '4'  '4'  '4'  '4'  '4'
 '5'  '5'  '5'  '5'  '5'  '5'  '5'  '5'  '5'
 '6'  '6'  '6'  '6'  '6'  '6'  '6'  '6'  '6'
 '7'  '7'  '7'  '7'  '7'  '7'  '7'  '7'  '7'
 '8'  '8'  '8'  '8'  '8'  '8'  '8'  '8'  '8'
 '9'  '9'  '9'  '9'  '9'  '9'  '9'  '9'  '9'
  • Related