Repeating one loop without adding data to the array

12 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Matt Fig
Matt Fig il 11 Ott 2012
Modificato: Matt Fig il 11 Ott 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 Commenti
Matt Fig
Matt Fig il 11 Ott 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by