# Convert a matrix to a dictionary

**URL:** <https://discourse.julialang.org/t/convert-a-matrix-to-a-dictionary/83872>\
**Category:** General Usage\
**Created:** [July 7, 2022, 3:13am UTC](https://discourse.julialang.org/t/convert-a-matrix-to-a-dictionary/83872 "2022-07-07T03:13:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosieG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josieg/32/36221_2.png) [@JosieG](https://discourse.julialang.org/u/JosieG)\
**Post date:** [July 7, 2022, 3:13am UTC](https://discourse.julialang.org/t/convert-a-matrix-to-a-dictionary/83872/1 "2022-07-07T03:13:16Z")

</div>

Suppose I have a 40_122 matrix, and a 122-element vector. I want to convert the matrix to a dictionary, in which keys are the elements of the 122-element vector, values are vectors of the 40_122 matrix. How can I get that? The reason that I do this is because I want to apply umap to the 40\*122 matrix. And I wanna know the labels of points in umap. Is there anyway to get that?  
My code for umap is

> embedding = umap(mat, 2; n\_neighbors=15, min\_dist=0.001, n\_epochs=200)  
> Plots.scatter(embedding[1,:],embedding[2,:])

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [July 7, 2022, 3:51am UTC](https://discourse.julialang.org/t/convert-a-matrix-to-a-dictionary/83872/2 "2022-07-07T03:51:19Z")

</div>

```julia
julia> v = [1, 2, 3, 4, 5]
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> M = rand(4,5)
4×5 Matrix{Float64}:
 0.695742 0.106581 0.313914 0.914068 0.99514
 0.844412 0.570495 0.828769 0.61195 0.396624
 0.178708 0.899122 0.826838 0.269306 0.221348
 0.0225007 0.953077 0.922949 0.11589 0.378226

julia> d = Dict(v[i] => M[:,i] for i in 1:length(v))
Dict{Int64, Vector{Float64}} with 5 entries:
  5 => [0.99514, 0.396624, 0.221348, 0.378226]
  4 => [0.914068, 0.61195, 0.269306, 0.11589]
  2 => [0.106581, 0.570495, 0.899122, 0.953077]
  3 => [0.313914, 0.828769, 0.826838, 0.922949]
  1 => [0.695742, 0.844412, 0.178708, 0.0225007]

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [July 7, 2022, 7:11am UTC](https://discourse.julialang.org/t/convert-a-matrix-to-a-dictionary/83872/3 "2022-07-07T07:11:34Z")

</div>

More concisely, this works as well, although the type-inference of the values only works correctly on unreleased versions:

```julia
julia> v = [1, 2, 3, 4, 5];

julia> M = rand(4,5);

julia> Dict(zip(v, eachcol(M)))
Dict{Int64, SubArray{Float64, 1, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}} with 5 entries:
  5 => [0.953176, 0.970684, 0.970148, 0.0255689]
  4 => [0.180715, 0.497071, 0.661425, 0.453757]
  2 => [0.452738, 0.0332605, 0.673278, 0.399781]
  3 => [0.435803, 0.808004, 0.727174, 0.151896]
  1 => [0.630868, 0.139696, 0.527969, 0.942606]

```

On julia v1.7, one may use

```julia
Dict{Int, typeof(view(M,:,1))}(zip(v, eachcol(M)))

```

to explicitly specify the types of the values. Note that in all these cases, we’re using views of the array instead of constructing the slices.
