Repeating one loop without adding data to the array
조회 수: 12 (최근 30일)
이전 댓글 표시
Hi all,
I've got a for-loop that randomises the '1's in one of the columns in a 10-by-6 matrix each of 6 loops. In another function I have a measure for nestedness of the matrix (nestedloop2), which can be somewhere from 1-to-100. I check nestedness before randomising the ones in a column, and after (oldnest vs. newnest).
The problem I have is that I only want the for-loop to continue if nestedness decreases. In other words, I only want to add 'newnest' to the 'nest'-array if 'newnest < oldnest'. I have tried using an if-statement or a while-loop, but I'm doing something wrong. Script:
for i=1:6;
oldnest=nestedloop2(H)
COLNOW=find(COL==i);
ii=H(:,COLNOW);
ii(randperm(10))=ii;
H(:,COLNOW)=ii;
newnest=nestedloop2(H)
nest=[nest,newnest];
end
I hope my description is a bit clear. Thanks in forward.
Cheers, T.
댓글 수: 0
채택된 답변
Matt Fig
2012년 10월 11일
편집: Matt Fig
2012년 10월 11일
From your description, you only want the variable 'nest' to grow and the FOR loop to continue if newnest is less than oldnest.
for i=1:6;
oldnest=nestedloop2(H)
COLNOW=find(COL==i);
ii=H(:,COLNOW);
ii(randperm(10))=ii;
H(:,COLNOW)=ii;
newnest=nestedloop2(H)
if newnest<oldnest
nest=[nest,newnest];
else
break
end
end
댓글 수: 3
Matt Fig
2012년 10월 11일
Then you do not want a FOR loop.
cnt = 1;
while cnt<=6
oldnest=nestedloop2(H)
COLNOW=find(COL==cnt);
ii=H(:,COLNOW);
ii(randperm(10))=ii;
H(:,COLNOW)=ii;
newnest=nestedloop2(H)
if newnest<oldnest
nest=[nest,newnest];
cnt = cnt + 1;
end
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!