MATLAB: Repeating one loop without adding data to the array

for-loop matrix repeat loop

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.

Best Answer

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