Search code examples
matlabadjacency-matrix

Filtering an adjacency matrix in matlab


I have got a nx3 adjacency matrix that contains nodes in the first two dimension and the correspondant weight in the third dimension. I want to filter the matrix for specific thresholds (for nodes indexing). For example, I want to keep the adjacency matrix for nodes smaller than 10.000, 20.000, etc. Which is the most efficient way to do so in matlab? I tried to do the following, find the index which correspond to nodes:

counter = 1;
for i=1: size(graph4, 1)
   if (graph4(i,1) >30000) | (graph4(i,2) >30000)
      bucket(counter) = i;
      counter=counter+1;
   end
end

Solution

  • Suppose the adjacency matrix is A as given below:

    A =    
        8    1    6
        3    5    7
        4    9    2
       11    4    9
        6    8   10
        7   12    5
       17   10   15
       12   14   16
       13   18   11
    

    If you want both column 1 and column 2 to be less than a value, you can do:

    value = 10;
    T = A(A(:,1) < value & A(:,2) < value, :)
    T =    
        8    1    6
        3    5    7
        4    9    2
        6    8   10
    

    The following line seems to give the same results as your sample code (but it doesn't seem like it fits your description.

    value = 10000;
    bucket = find((A(:,1)>value) | A(:,2)>value)
    

    I guess you made a mistake and want to increment the counter above the bucket-line and initialize it as counter = 0 before the loop? As it is now, it will be one more than the number of elements in the bucket-list.