Creating an adjacency matrix from a table

8 views (last 30 days)
William Sheehan
William Sheehan on 7 Aug 2019
Answered: Steven Lord on 12 Aug 2019
I am trying to create a weighted adjacency matrix that counts the number of times two individuals interact so that I can subsequently create a sparse variable and digraphs.
I have two columns of individual names, each with 182 rows. Each row is indicative of an interaction.
Column 1 contains the individual that distributed the information whereas column 2 contains the name of the individual who recieved the information similar to below:
Screenshot (124).png

Answers (2)

Hari Krishna Ravuri
Hari Krishna Ravuri on 12 Aug 2019
You may consider maintaining a lookup table or anything similar, in order to map the sender and receiver with their respective indices in the adjacency matrix. You can get the sender and the receiver by reading each row in the excel file using readtable() and increase the value of the element, corresponding to the sender and the receiver in the adjacency matrix. Please refer to the documentation on readtable() from the following link https://in.mathworks.com/help/matlab/ref/readtable.html.

Steven Lord
Steven Lord on 12 Aug 2019
You can create a digraph by passing in arrays of source node names and target node names. See the sections of the Syntax section on the digraph documentation page that list s and t as inputs.
Read your data into char, string, or cellstr arrays and create the digraph by passing the labels in as s and t. If you need the adjacency matrix, call adjacency on your digraph. Depending on what else you want to do, since your digraph is a multigraph (you have the pairs [A, B] and [C, B] listed twice each in your spreadsheet) you may need or want to call simplify on it first.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!