【发布时间】:2022-01-21 01:47:22
【问题描述】:
如何在 Julia 中将二维元组添加到二维矩阵?
t1 = ((10,20),(30,40)); #2d immutable tuple
a = [1 2;3 4] #2d matrix
a .+ t1
抛出错误:
MethodError: no method matching +(::Int64, ::Tuple{Int64, Int64})
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
+(::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:87
+(::Integer, ::Ptr) at pointer.jl:161
...
Stacktrace:
[1] _broadcast_getindex_evalf
@ .\broadcast.jl:648 [inlined]
[2] _broadcast_getindex
@ .\broadcast.jl:621 [inlined]
[3] getindex
@ .\broadcast.jl:575 [inlined]
[4] copy
@ .\broadcast.jl:922 [inlined]
[5] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{2}, Nothing, typeof(+), Tuple{Matrix{Int64}, Tuple{Tuple{Int64, Int64}, Tuple{Int64, Int64}}}})
@ Base.Broadcast .\broadcast.jl:883
[6] top-level scope
@ REPL[15]:1
是否存在向量/矩阵相加方法? 显然,我可以使用 for 循环逐个元素添加。
【问题讨论】:
标签: matrix tuples julia arithmetic-expressions