# Convert a matrix to a dictionary

Suppose I have a 40122 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 40122 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,:])

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

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

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

``````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.