在朱莉娅(Julia)中,如何初始化一个二维数组(一个矩阵),其中每个元素是列向量的?

发布于 2025-02-13 04:12:30 字数 752 浏览 2 评论 0原文


使用Julia版本1.7.3,我的理解是,以下初始化矩阵是9×9矩阵每个元素的元素不确定……

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

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

另外,我知道expression for c for c for c for C ='1':'9']评估9个字符的列向量(类型vector {char})。


如何将上述组合以创建一个9×9矩阵,每个矩阵的81个元素是列vector ['1'; '2'; '3'; '4'; '5'; '6'; '7'; '8'; '9']? (我的期望是,我计划编写的程序中将修改每个列向量元素。) 到目前为止,我最好的猜测是……

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

试图这样做会导致…

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


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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平定天下 2025-02-20 04:12:30

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']

请注意,数组 s可以是多维的(超过2个),因此也是一个3维数组。也只需要一个小更改:[C for C ='1':'9',_ in 1:9,_ in 1:9]


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]

一杆小烟枪 2025-02-20 04:12:30

这是你想要的吗?

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'

或者

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'

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文