There is a problem in loop. Could anyone please correct my code.
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
KSSV
2017-4-11
What you want to compare? What you want to do after comparing? Your question is not clear.
采纳的回答
Guillaume
2017-4-11
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 个评论
Guillaume
2017-4-13
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))
更多回答(1 个)
Andrei Bobrov
2017-4-11
编辑:Andrei Bobrov
2017-4-12
[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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!