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

**URL:** <https://discourse.julialang.org/t/assigning-value-from-a-matrix-to-a-dictionary-with-key-as-pairs-i-j/98548>\
**Category:** New to Julia\
**Tags:** dictionaries\
**Created:** [May 9, 2023, 12:41pm UTC](https://discourse.julialang.org/t/assigning-value-from-a-matrix-to-a-dictionary-with-key-as-pairs-i-j/98548 "2023-05-09T12:41:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gagan\_Meena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gagan_meena/32/49621_2.png) [@Gagan\_Meena](https://discourse.julialang.org/u/Gagan_Meena)\
**Post date:** [May 9, 2023, 12:41pm UTC](https://discourse.julialang.org/t/assigning-value-from-a-matrix-to-a-dictionary-with-key-as-pairs-i-j/98548/1 "2023-05-09T12:41:47Z")

</div>

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  
end  
end

struct Ld  
PG::Dict{Pair{Int16,Int16}, Float16}  
PL::Dict{Int16, Float16}  
dur::Matrix{Int16}  
end  
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  
…

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [May 9, 2023, 12:57pm UTC](https://discourse.julialang.org/t/assigning-value-from-a-matrix-to-a-dictionary-with-key-as-pairs-i-j/98548/2 "2023-05-09T12:57:18Z")

</div>

> [@Gagan\_Meena](#):
>
> Load = Ld(Pg\_Dict, Pd\_Dict, dur)

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

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [May 9, 2023, 1:02pm UTC](https://discourse.julialang.org/t/assigning-value-from-a-matrix-to-a-dictionary-with-key-as-pairs-i-j/98548/3 "2023-05-09T13:02:52Z")

</div>

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:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 9, 2023, 7:47pm UTC](https://discourse.julialang.org/t/assigning-value-from-a-matrix-to-a-dictionary-with-key-as-pairs-i-j/98548/4 "2023-05-09T19:47:38Z")

</div>

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

```julia
m=rand(10,15)

d=Dict{Tuple{Int,Int},Float64}()
[d[ci.I]=m[ci] for ci in CartesianIndices(m)]

```

---

<div class="post-metadata">

**Author:** ![Gagan\_Meena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gagan_meena/32/49621_2.png) [@Gagan\_Meena](https://discourse.julialang.org/u/Gagan_Meena)\
**Post date:** [May 10, 2023, 4:48am UTC](https://discourse.julialang.org/t/assigning-value-from-a-matrix-to-a-dictionary-with-key-as-pairs-i-j/98548/7 "2023-05-10T04:48:23Z")

</div>

```julia
dur::Matrix{Int16}

```
