Problem in Finding COnnected Components: Error Matrix need to be sparse. I have 2500 cells with 150 1's. Still showing matrix is not sparse. Please help me to generate connected compnents of the disconnected graph
4 views (last 30 days)
Show older comments
Beneyaz Begum
on 8 Mar 2016
Commented: Beneyaz Begum
on 8 Mar 2016
I am writing below the error message reflected:
??? Error using ==> check_matlab_bgl at 50
the matrix A must be sparse. (See set_matlab_bgl_default.)
Error in ==> components at 39
if check, check_matlab_bgl(A,struct()); end
Error in ==> StoreADJMatrix at 83
comps = components(adjmat)
%%%%%%%%%%%%%%Part of the code
% I have also attached matfile having adjmat matrix for no of nodes 50 in 200X 200 area
R=29;
row=1;
E=zeros(0,0);
format short g
for i=1:noOfNodes
for j=1:noOfNodes
distance(i,j) = sqrt((Xd(i)-Xd(j))^2 + (Yd(i)-Yd(j))^2);
if distance (i,j) <= R && (i~=j)
% row=row+1;
matrix(i, j) = 1; % there is a link;
E(row,1)=i;
E(row,2)=j;
E(row,3)=distance(i,j);
row=row+1;
else
matrix(i,j) = 0;
end;
end;
end
save('NodeGraph.mat','matrix','distance','E','-append')
%for each pair (i,j) in the connectivity list E, set adj(i,j) to 1
fromlist = E(:,1);
tolist = E(:,2);
adjmat=zeros(noOfNodes,noOfNodes);
fromtoidx = sub2ind([noOfNodes,noOfNodes], fromlist, tolist); %as linear indices
adjmat(fromtoidx) = 1;
tofromidx = sub2ind([noOfNodes,noOfNodes], tolist, fromlist); %connections are bidirectional
adjmat(tofromidx) = 1;
save('NodeGraph.mat','adjmat','-append')
%nMST=grMinSpanTree(E)
gplot(adjmat,coords(:,:),'-*')
grid on
axis square
comps = components(adjmat)
%%%%%%%%%%%%%%
0 Comments
Accepted Answer
Walter Roberson
on 8 Mar 2016
Replace
adjmat=zeros(noOfNodes,noOfNodes);
with
adjmat = sparse([], [], [], noOfNodes, noOfNodes);
However, you would be even better off using sparse() calls to do the assignments with no linear indexing and no explicit assignments:
adjmat = sparse([fromlist; tolist], [tolist; fromlist], 1);
More Answers (0)
See Also
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!