Search a Table With Numerical Entries

6 次查看(过去 30 天)
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.
  1 个评论
Kamal Premaratne
Kamal Premaratne 2020-5-29
Just a follow-up: My objective is to create a digraph (directed graph) from the data in the original table.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2020-5-29
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 个评论
Kamal Premaratne
Kamal Premaratne 2020-5-29
Yep, this should work. Let me try on the big table. Thank you so much Streven.
Kamal Premaratne
Kamal Premaratne 2020-5-30
It works fine on the dataset. I was a bit concerned because the total number of nodes of the digraph being generated was less than the number of variables (columns) of the table. But then I realized that this might be due to the fact that there are isolated nodes (nodes that are not connected any other node) which we do not capture because we only pick edges with non-zero weights.
Again, Steve, thank you so much.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by