Create matlab adjacency matrix

We apologize for the formatting and, perhaps, a very simple question. I am new to matlab and this stack exchange. I am trying to create an adjacency matrix from multiple columns in matlab. Information was imported from a text file. The information looks like this.

XYZW aa bb 1 aa bb cc 2 bb cc dd 3 cc 

If the columns X and Y are the names of the vertex columns. Z is the weight. Columns X and Y have about 30000 repetitive records. Column W is all the vertices in my graph, sorted alphabetically without repetition. The result should look like the example data.

  aa bb cc dd aa 0 1 0 0 bb 1 0 2 0 cc 0 2 0 3 dd 0 0 3 0 

I know how to create a matrix if the vertices are numeric. But I can’t figure out how to assign numerical values ​​to the vertices in column W and make everything still match.

This code will work if the values ​​in all columns are numeric.

 A = sparse([X; Y],[Y; X],[Z; Z]); 

Where X, Y and Z are the columns above. When I try to do this, I get the following error

 'Undefined function 'sparse' for input arguments of type 'cell' 
+5
source share
1 answer

You can still use sparse but you have to do a little more work. First, we need to convert the labels in X and Y to unique integer identifiers. Try using unique combinations of inputs X and Y so that you can get unique integer identifiers shared by both.

In particular, unique will give you a list of all unique input entries (so that X and Y combined). The reason we combine X and Y is because there are certain tokens in X that may not be in Y and vice versa. Performing this assignment of identifier on the combined input will ensure consistency. The 'stable' flag is present because unique actually sorts all unique entries by default. If the input is an array of row cells, the array of cells is sorted in lexicographic order. If you want to preserve the order in which unique records are encountered, from the beginning to the end of the cell array, you use the 'stable' flag.

Next, I would use an associative array through containers.Map which maps the string to a unique integer. Think of the associative array as a dictionary in which the input is the key and the output is the value associated with this key. The best example of an associative array in this context would be an English dictionary. The key in this case is the word you want to find, and the meaning is the definition of the word. The key is a character string, and the output is another character string.

Here we will make the input a string, and the output - a single number. For each unique row that we encounter a combination of X and Y , we will assign it a unique identifier. After that, we can use X and Y as input for containers.Map to get our identifiers, which can then be used as input for sparse .

Without further ado, here is the code:

 %// Your example X = {'aa', 'bb', 'cc'}; Y = {'bb', 'cc', 'dd'}; Z = [1 2 3]; %// Call unique and get the unique entries chars = unique([XY], 'stable'); %// Create containers.Map map = containers.Map(chars, 1:numel(chars)); %// Find the IDs for each of X and Y idX = cell2mat(values(map, X)).'; idY = cell2mat(values(map, Y)).'; %// Create sparse matrix A = sparse([idX; idY], [idY; idX], [Z; Z]); 

The third and second last lines of code are a little peculiar. You need to use the values function to get the values ​​from the key array. We have X and Y as both cell arrays, and therefore the output is also an array of value cells. We don’t want it to be an array of cells, but a numeric vector instead of sparse input, so we use cell2mat to convert this back to us. Once we finally get the identifiers for X and Y , we put this in sparse to complete the matrix.

When we show the full version of A , we get:

 >> full(A) ans = 0 1 0 0 1 0 2 0 0 2 0 3 0 0 3 0 

Minor note

I see that W is an array of vertex name cells sorted alphabetically. If so, then you do not need to make any unique calls, and you can simply use W as input to containers.Map . containers.Map . Do this:

 %// Create containers.Map map = containers.Map(W, 1:numel(W)); %// Find the IDs for each of X and Y idX = cell2mat(values(map, X)).'; idY = cell2mat(values(map, Y)).'; %// Create sparse matrix A = sparse([idX; idY], [idY; idX], [Z; Z]); 
+5
source

All Articles