How to avoid an Infinite Loop?
Show older comments
Hi,
I'm encountering an infinite loop problem. I tried to debug my code but couldn't figure it out, so I would appreciate your thoughts and ideas.
[nComponents,sizes,members] = networkComponents(NetA);
[nComponentsB,sizesB,membersB] = networkComponents(NetB);
while sizes(1)~=sizesB(1)
for ii=1:Nodes
if nnz(NetA(ii,:))<1 && nnz(NetA(:,ii))<1 && ~ismember(ii,attack)
C(ii,:)=0; % All nodes in Layer B coupled to neighboring nodes will get failed
indab=ismember(inda,ii);
indab=find(indab);
NetB(indb(indab),:)=0;
NetB(:,indb(indab))=0;
end
end
% Propagation back to Layer A
for pp=1:Nodes
if nnz(NetB(pp,:))<1 && nnz(NetB(:,pp))<1
C(:,pp)=0;
indba=ismember(indb,pp);
indba=find(indba);
NetA(inda(indba),:)=0;
NetA(:,inda(indba))=0;
end
end
end
Where,
"NetA" and "NetB" are square symmetric matrices.
"inda" and "indb" are row vectors of the same size.
"attack" and "Nodes" are positive integers.
"sizes(1)" and "sizesB(1)" are positive integers.
1 Comment
per isakson
on 7 Nov 2020
Edited: per isakson
on 7 Nov 2020
You increases your chances to get a prompt answer if you provide sample data so that we easier can run your code.
Answers (1)
David Goodmanson
on 7 Nov 2020
Edited: David Goodmanson
on 7 Nov 2020
0 votes
Waseem,
Nothing in the entire while loop alters either 'sizes' or 'sizesB'. So if sizes(1) ~= sizesB(1), that condition will not ever change, leading to an infinite while loop. For the loop to terminate, you will have to change either sizes(1) or sizesB(1) within the loop, in such a way that the two eventually are equal.
4 Comments
Waseem AL Aqqad
on 7 Nov 2020
David Goodmanson
on 7 Nov 2020
Edited: David Goodmanson
on 7 Nov 2020
Hi Waseem,
If it's true that sizes = size(NetA) then it's easy. Just insert that statement into the while loop just after the changes are made to netA, same for sizesB and netB. If sizes has some more complicated relationship to netA then you will have to call up whatever function is going to give you that answer.
ADDED:
Additionally, of course the true-false check will have to pass in one or both of the 'if' loops in order for netA or netB to change.
Waseem AL Aqqad
on 7 Nov 2020
Edited: Waseem AL Aqqad
on 7 Nov 2020
Waseem AL Aqqad
on 8 Nov 2020
Categories
Find more on Matrix Indexing 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!