Repeating one loop without adding data to the array
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Matt Fig
on 11 Oct 2012
Edited: Matt Fig
on 11 Oct 2012
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 Comments
Matt Fig
on 11 Oct 2012
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
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!