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

per isakson
per isakson 2020년 11월 7일
편집: per isakson 2020년 11월 7일
You increases your chances to get a prompt answer if you provide sample data so that we easier can run your code.

댓글을 달려면 로그인하십시오.

답변 (1개)

David Goodmanson
David Goodmanson 2020년 11월 7일
편집: David Goodmanson 2020년 11월 7일

0 개 추천

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

Waseem AL Aqqad
Waseem AL Aqqad 2020년 11월 7일
Thanks for your reply, David!
So do I have to call the function "networkComponents" within the while loop? as sizes(1) and sizesB(1) depend on NetA and NetB which they do change within the loop.
David Goodmanson
David Goodmanson 2020년 11월 7일
편집: David Goodmanson 2020년 11월 7일
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
Waseem AL Aqqad 2020년 11월 7일
편집: Waseem AL Aqqad 2020년 11월 7일
Hi David,
Unfortunately, that didn't work. I will re-debug my whole codes tomorrow morning and let you know.
Thanks!
Hi David,
Besides having to pass "networkComponents" in while loop, I should have imposed one constraint to my algorithm to avoid the infinite loop (both networks NetA and NetB should have same average degree).
Say I have two row vectors with the same size but might have different sum of elements as I'm generaing them randomly.
rowA=[3 6 2 4 3 1 1 1 1 2 2 2 3 1 1 1 1 1 1 1]
rowB=[5 5 5 5 6 2 2 1 1 1 2 1 1 1 1 1 1 1 1 1]
%sum(rowA) should equal sum(rowB). so, I want to manipulate the elements of the vector
% with the bigger sum in order to satisfy this constraint but their sizes should remain
% the same.
% Perhaps, the code should replace some of the ones with zeros. As no. 1 will
% have the most frequent occurance in both vectors in each simulation run.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2020년 11월 7일

편집:

2020년 11월 8일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by