Repeating one loop without adding data to the array

1 回表示 (過去 30 日間)
Tim
Tim 2012 年 10 月 11 日
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.

採用された回答

Matt Fig
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
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
Tim
Tim 2012 年 10 月 11 日
Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by