Just a follow-up: My objective is to create a digraph (directed graph) from the data in the original table.
Search a Table With Numerical Entries
6 views (last 30 days)
Show older comments
Kamal Premaratne
on 29 May 2020
Commented: Kamal Premaratne
on 30 May 2020
I have a table which has only numerical (actually integer) entries. Each row and column are indexed by a string. As an example,
I1L I1R I2L I2R I3 I4
----- ----- ----- ----- ----- -----
I1L 0 0 2 0 1 0
I1R 0 0 0 1 1 0
I2L 10 0 0 3 0 0
I want to be able to identify the non-zero entries and then create a 3-column table which has the row name, column name, and the integer value. For example,
I1L I2L 2
I1L I3 1
I1R I2R 1
I1R I3 1
I2L I1L 10
I2L I2R 3
Of course, the table that I am working with is quite large. I have been trying various combinations of commands, all in vain I'm afraid. I would very much appreciate any assistance. Thank you.
Accepted Answer
Steven Lord
on 29 May 2020
If you want to make a digraph from your table, you don't need to go to the three-column table first. Let's make a sample table in the same form as what you have. Let's start off with 20 non-zero values at random locations in a 10-by-10 array.
ind = randi(100, 1, 20);
weight = randi(10, 1, 20);
A = zeros(10, 10);
A(ind) = weight;
Turn this into a table array with variable names A1, A2, ... [Yes, I know defining stand-alone variables with numbered names is discouraged. Letting MATLAB automatically make variables inside a table array with numbered names like this doesn't clutter the namespace, so I'm okay with that.]
T = array2table(A)
Now build your digraph. I assumed you want the node names in the digraph to be the variable names in the table.
D = digraph(T.Variables, T.Properties.VariableNames)
If you only wanted some of the rows and variables to be present:
selection = 1:2:width(T);
D2 = digraph(T{selection, selection}, T.Properties.VariableNames(selection))
5 Comments
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!