MATLAB: There is a problem in loop. Could anyone please correct the code.

doit4me

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.

Best Answer

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.