Assigning value from a matrix to a dictionary with key as pairs(i,j)

Pd = Pd_max.*load_rat
Pdm = sum(Pd, dims=1)

Pg_Dict = Dict{Pair{Int16,Int16}, Float16}()
Pd_Dict = Dict{Int16, Float16}()

for i =1:n_bus
for j =1:n_scenarios
key = Pair(i,j)
Pd_Dict[j] = Pdm[j]
value = Pg[i,j]
Pg_Dict[key] = value

struct Ld
PG::Dict{Pair{Int16,Int16}, Float16}
PL::Dict{Int16, Float16}
Load = Ld(Pg_Dict, Pd_Dict, dur)
Here Pg is a matrix of (24, 4)

I am getting following error.

MethodError: Cannot convert an object of type Dict{Pair{Int16, Int16}, Float16} to an object of type Matrix{Float16}
Closest candidates are:
convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray at C:\Users\Admin\AppData\Local\Programs\Julia-1.8.5\share\julia\stdlib\v1.8\LinearAlgebra\src\factorization.jl:58
convert(::Type{Array{T, N}}, ::StaticArraysCore.SizedArray{S, T, N, N, Array{T, N}}) where {S, T, N} at C:\Users\Admin.julia\packages\StaticArrays\VLqRb\src\SizedArray.jl:88
convert(::Type{Array{T, N}}, ::StaticArraysCore.SizedArray{S, T, N, M, TData} where {M, TData<:AbstractArray{T, M}}) where {T, S, N} at C:\Users\Admin.julia\packages\StaticArrays\VLqRb\src\SizedArray.jl:82

what is the value of dur? (doesn’t seem to be in the code)

As @Dan indicated, the problem is likely in the dur variable. It appears to be of type Dict{Pair{Int16, Int16}, Float16} but your structure expects type Matrix{Float16}. Therefore, when you call the constructor Ld(Pg_Dict, Pd_Dict, dur), Julia implicitly invokes convert(Matrix{Float16}, dur), which fails.

Also, please, read the following instructions and try to adapt your posts accordingly:

I answer only on the title of the object. I haven’t read the code


[d[ci.I]=m[ci]   for ci in CartesianIndices(m)]
1 Like