Search a Table With Numerical Entries

조회 수: 11 (최근 30일)
Kamal Premaratne
Kamal Premaratne 2020년 5월 29일
댓글: Kamal Premaratne 2020년 5월 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에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by