There is a problem in loop. Could anyone please correct my code.
Afficher commentaires plus anciens
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 commentaire
KSSV
le 11 Avr 2017
What you want to compare? What you want to do after comparing? Your question is not clear.
Réponse acceptée
Plus de réponses (1)
Andrei Bobrov
le 11 Avr 2017
Modifié(e) : Andrei Bobrov
le 12 Avr 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 commentaires
SUNANNA S S
le 12 Avr 2017
Andrei Bobrov
le 12 Avr 2017
Modifié(e) : Andrei Bobrov
le 12 Avr 2017
of course
SUNANNA S S
le 12 Avr 2017
Andrei Bobrov
le 12 Avr 2017
Yes, error.
Please see answer by Guillaume.
Catégories
En savoir plus sur Performance and Memory dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
