# How to construct a MetaGraph from a weighted adjacency matrix

**URL:** https://discourse.julialang.org/t/how-to-construct-a-metagraph-from-a-weighted-adjacency-matrix/23996
**Category:** Graphs
**Tags:** lightgraphs
**Created:** [May 8, 2019, 2:28pm UTC](https://discourse.julialang.org/t/how-to-construct-a-metagraph-from-a-weighted-adjacency-matrix/23996 "2019-05-08T14:28:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![pegger0709](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pegger0709/32/1811_2.png) [@pegger0709](https://discourse.julialang.org/u/pegger0709)
#### Post date: [May 8, 2019, 2:28pm UTC](https://discourse.julialang.org/t/how-to-construct-a-metagraph-from-a-weighted-adjacency-matrix/23996/1 "2019-05-08T14:28:28Z")

</div>

Hello,  
I am a researcher studying structural brain networks of stroke patients. I have some connectomes which are given as symmetric weighted matrices, e.g. (as a random example)

```julia
julia> X = rand(300,300); C = Symmetric((X + transpose(X)) / 2 - Diagonal(X))

```

I want to create a MetaGraph in which the edges have the weights in the matrix. In addition, I have a list of anatomical names for the 300 brain regions in the connectome, which I would like to serve as labels for the nodes. I can’t find where the documentation tells us how to do this. Any help would be greatly appreciated.  
Thanks so much,  
Philip

---

<div class="post-metadata">

### Author: ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)
#### Post date: [May 8, 2019, 2:54pm UTC](https://discourse.julialang.org/t/how-to-construct-a-metagraph-from-a-weighted-adjacency-matrix/23996/2 "2019-05-08T14:54:36Z")

</div>

I’m not aware of a function to load a `MetaGraph` with weights from an Array. You can do something like this to achieve what you are looking for.

```julia
function weightedmetagraph(C)
    g=MetaGraph(size(C,1))
    for ind in CartesianIndices(C)
        if ind[1] != ind[2]
             add_edge!(g,ind[1],ind[2],:weight,C[ind])
        end
    end
    g
end

```

As for labels, you either have to set up a label when the vertex is added with  
`add_vertex!(g,:label,"Node Label")`, or after the fact with  
`set_prop!(g, vertex, :label, "Node Label")`.

The `:weight` label is the only label that has prescribed functionality, but that can also be changed with the `weightfield!` function.

---

<div class="post-metadata">

### Author: ![pegger0709](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pegger0709/32/1811_2.png) [@pegger0709](https://discourse.julialang.org/u/pegger0709)
#### Post date: [May 9, 2019, 11:41am UTC](https://discourse.julialang.org/t/how-to-construct-a-metagraph-from-a-weighted-adjacency-matrix/23996/3 "2019-05-09T11:41:58Z")

</div>

Thanks, appreciate it!
