There is a problem in loop. Could anyone please correct my code.
显示 更早的评论
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.
采纳的回答
更多回答(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 个评论
SUNANNA S S
2017-4-12
Andrei Bobrov
2017-4-12
编辑:Andrei Bobrov
2017-4-12
of course
SUNANNA S S
2017-4-12
Andrei Bobrov
2017-4-12
Yes, error.
Please see answer by Guillaume.
类别
在 帮助中心 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
