Azzera filtri
Azzera filtri

There is a problem in loop. Could anyone please correct my code.

1 visualizzazione (ultimi 30 giorni)
I want to find the 1's in first column and compare it with all other columns and if there is a matching column then count 1 and then second column with remaining columns and so on, but it's not working in my code. I find the value of only one column in this code.
function CNmat=getCNMatrix(adj,col)
clc;
adj=[0 1 1 1 1 0; 1 0 1 1 0 1; 0 0 0 1 0 1; 1 0 1 0 0 1; 1 0 0 1 0 1; 0 0 0 0 1 0];
col=1;
[r,c]=size(adj);
%for col=1:c
[xi,xj]=find(adj(:,col)==1);
withOne=adj(xi,:);
[zr,zc]=find(withOne==1);
for j=1:c
if (j==col)
continue;
end
CNmat(j)=length(find(zc==j));
end
For eg:,
The result of this code is : ans =
0 0 2 2 0 3
It means that when I take the 1's of first column and compare it with other columns, there is no corresponding elements are same in column 2 with column 1.On the other hand, on columns 3 and 4 there are 2 1's same as in column 1 and on column 6 there are 3 1's matches with column 1.But, I want this as a matrix by taking each column's 1 and match it with succeeding columns.
  1 Commento
KSSV
KSSV il 11 Apr 2017
What you want to compare? What you want to do after comparing? Your question is not clear.

Accedi per commentare.

Risposta accettata

Guillaume
Guillaume il 11 Apr 2017
It is very hard to understand what it is you want as you don't tell us what result you want. Really, the best for you would be to give us an example input and the corresponding output that should be generated with an explanation of how you go from one to the other.
I'm completely guessing here, based on your hazy description and your incomplete code, that maybe you want this:
adj = [0 1 1 1 1 0; 1 0 1 1 0 1; 0 0 0 1 0 1; 1 0 1 0 0 1; 1 0 0 1 0 1; 0 0 0 0 1 0];
CNmat = zeros(size(adj, 2));
for col = 1:size(adj, 2)
filteredadj = adj .* adj(:, col); %only keep the ones whose rows match the ones in the current column
filteredadj(:, col) = 0; %ignore current column
CNmat(col, :) = sum(filteredadj); %sum the ones in each column
end
CNmat(col, :) is the count of 1 in each column of adj that are in the same row as column col. No idea if that's the result you want. You'll see that CNmat(1, :) matches the output of the code you've posted.
  15 Commenti
SUNANNA S S
SUNANNA S S il 13 Apr 2017
Sir, I tried all these code as per your guidance, but I only get a zero matrix as output. I don't know what is the problem. I had attached my files with this.Please help me.
Guillaume
Guillaume il 13 Apr 2017
Oh! We're back to twitnet. A few comments then,
Your twitnet edge file appears to be for an undirected graph (the adjacency matrix is symmetric) since there's no direction information, whereas your example above is a directed graph (non-symmetric adjacency matrix).
For an undirected graph, it makes no sense to me to only keep the upper diagonal of CNmat. CNmat is going to be symmetric anyway.
If the only reason you're calculating the adjacency matrix is to obtain your CNmat, then you could just obtain CNmat directly and possibly faster from the edge matrix.
Anyway, "I only get a zero matrix as output". No, you don't.
find(CNmat)
will show you the location of all the non-zeros values in CNmat. There's not many but given your graph what else did you expect. First have a look at the graph of your twitnet:
There isn't a single cycle and it's extremely disconnected. Now what you are calculating with your CNmat is for each pair of nodes how many nodes do they both connect to. For the overwhelming majority of nodes pairs, that is zero. For the few that actually form a graph, since all these graphs are star shaped, the maximum number of common nodes for a pair is at most 1.
That's exactly the CNmat that Andrei's and my answer produce. For example, look at the 105th node (node '301180' in twitnet), which one of the point of one of the 7-pointed stars:
>> find(CNmat(105, :)
ans =
106 107 108 109 110 111
It connects exactly to one common node (104) with the other nodes (106, 107, etc.).
As advised in the other thread, I would recommend you upgrade to a newer version of matlab as plotting the graph as I've done above is trivial in R2015b or later:
plot(graph(X))

Accedi per commentare.

Più risposte (1)

Andrei Bobrov
Andrei Bobrov il 11 Apr 2017
Modificato: Andrei Bobrov il 12 Apr 2017
[EDIT]
CNmat =...
triu(squeeze(sum(adj.*permute(adj,[1 3 2]))).*~eye(size(adj,2)));%R2016b and later
CNmat = triu(squeeze(sum(...
bsxfun(@times,adj,permute(adj,[1 3 2])))).*~eye(size(adj,2))); % For early versions
  4 Commenti
SUNANNA S S
SUNANNA S S il 12 Apr 2017
I got this error when I run this code.
Error using bsxfun
Out of memory. Type HELP MEMORY for your options.
Error in new (line 11)
CNmat = triu(squeeze(sum(bsxfun(@times,X,permute(X,[1 3 2])))).*~eye(size(X,2))); %sum
the ones in each column

Accedi per commentare.

Categorie

Scopri di più su Networks in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by