-1

I have an adjacency matrix(n*n) of 1's and 0's extracted from an unweighted and undirected graph, my goal is to remove all-zeros columns from this matrix and their corresponding rows which are not connected to any node from the graph.

I want to apply one algorithm by using this adjacency matrix but sadly NaN produces because of some columns in this matrix are all 0's. So, i only need the connected components.

fid= fopen('file.txt','rt');
format = repmat('%q',[1 2]);
filee= textscan(fid,format,'Delimiter', '\t');
fclose(fid);
AA2= [filee{:, 1} , filee{:, 2}];
[nodenames, ~, id] = unique(AA2(:));
Adjacency_Matrix= accumarray(reshape(id, size(AA2)), 1, [numel(nodenames), numel(nodenames)]);
Adjoint2 = sum(Adjacency_Matrix~=0,1);

https://drive.google.com/file/d/0B6u8fZadKIp2OFd2X1NrZEdIclU/view

By this command Adjoint2 = sum(Adjacency_Matrix3~=0,1); I can know how many 1's i have in every column. In this matrix, some columns had no 1's, and so don't want them.

As the matrix is (NN) i want to get a (mm) matrix with columns that has only the 1's.

F.caren
  • 85
  • 1
  • 9
  • 1
    Please provide a [mcve], and also, focus your question on the problem. Most of the code above has nothing to do with your adjacency matrix. – EBH Jun 19 '17 at 07:20
  • @ EBHi have added more explanation along with the the matrix i used. – F.caren Jun 19 '17 at 07:52
  • I think the the adjacency matrix should be created this way: `Adjacency_Matrix=dlmread('adjacency_matrix3.txt');` , however the resultant matrix is not symmetric. – rahnema1 Jun 19 '17 at 08:47

3 Answers3

3

You can use any function:

If the matrix is symmetric you can do this to remove both zero columns and rows:

idx = any(Adjacency_Matrix);
result = Adjacency_Matrix(idx,idx);

else you can generate indexes of both columns and rows:

idx_column = any(Adjacency_Matrix);
idx_row = any(Adjacency_Matrix,2);

Here you can remove both columns and rows

result = Adjacency_Matrix(idx_row, idx_column)

If you want to remove only columns use this:

result = Adjacency_Matrix(:, idx_column)

If you want to remove only rows use this:

result = Adjacency_Matrix(idx_row, :)
rahnema1
  • 15,264
  • 3
  • 15
  • 27
  • i tried your code it reduces the size of the matrix and by this code so many columns had 0's now – F.caren Jun 19 '17 at 07:14
  • I want to remove all zeros _columns in my previous matrix !; Adjoint2 = sum(Adjacency_Matrix~=0,1); by this command, I can know how many 1's i have in every column but some columns have no 1's and only 0's and so i want to remove them . – F.caren Jun 19 '17 at 07:15
  • 1
    Please post an example of , for example , a [5* 5] matrix and also the expected output. – rahnema1 Jun 19 '17 at 07:16
  • I posted a file example because by [5* 5] you can't understand how the matrix looks like and many thanks for your willingness to help me! – F.caren Jun 19 '17 at 07:51
  • @F.caren Adjacency matrix of a undirected graph should be symmetric but the posted txt file is not symmetric – rahnema1 Jun 19 '17 at 07:54
  • the text file of [95*95] with 1'0 and 0's, I extract this matrix from my graph .. 1 if there is an edge and 0 otherwise, So what's wrong ?? – F.caren Jun 19 '17 at 08:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147047/discussion-between-f-caren-and-rahnema1). – F.caren Jun 19 '17 at 10:16
  • i think the problem is in my matrix that's it not symmetric , i have used the code above to get the adjacency matrix but i got wrong matrix – F.caren Jun 19 '17 at 16:27
1

This is going to be a brute force solution, since you are using an adjacency matrix. You will have to loop through all of the rows of the matrix and determine which ones are empty, and create a new adjacency matrix from these rows, omitting the correct columns.

victor
  • 1,573
  • 11
  • 23
1

You can use the following code:

   i = 1;
   while(i<=length(A)) 
       if(sum(A(i,:)) == 0 && sum(A(:,i)) == 0)
             % node i is isolated
             A(:,i) = []; % remove its related column
             A(i,:) = []; % remove its related row
        else
             i = i + 1;
        end
   end

If sum of elements of a row and respected column is zero, it means this node is isolated. Therefore, you can remove its related row and column.

OmG
  • 18,337
  • 10
  • 57
  • 90
  • i try your code, but no results, the size of the data is still the same – F.caren Jun 19 '17 at 05:37
  • 1
    It means you have not any isolated node! So, you wanna find a will or source node. Do you? If it is, you can change the condition from && to ||. – OmG Jun 19 '17 at 05:42
  • i got this error Index exceeds matrix dimensions. Error in disjoint (line 192) if(sum(Adjacency_Matrixneighb2(i,:)) == 0 || sum(Adjacency_Matrixneighb2(:,i)) == 0) – F.caren Jun 19 '17 at 06:03
  • Sorry if i disturb you friend , i try the new code and still no change ! – F.caren Jun 19 '17 at 06:27
  • My pleasure. Did you get the Index exceeds matrix dimension error? – OmG Jun 19 '17 at 06:38
  • no, no error occurred! no bugs, and the size of the matrix remains the same – F.caren Jun 19 '17 at 06:39
  • So, it means you have not any will and source node. – OmG Jun 19 '17 at 06:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147026/discussion-between-f-caren-and-omg). – F.caren Jun 19 '17 at 06:52
  • OmG you know because my matrix in not symmetric that's why your code doesn't work, could you help to get the adjacency matrix?? – F.caren Jun 19 '17 at 16:26
  • adjacency matrix must be symmetric in definition. – OmG Jun 19 '17 at 21:35