How to define variable sets for changing value of i ?

1 回表示 (過去 30 日間)
MANISH KUMAR
MANISH KUMAR 2018 年 1 月 28 日
回答済み: Star Strider 2018 年 1 月 28 日
for Example, in below code, I defined 'DSet' and 'DCount' initially. I want to define these sets for each value of i.
for i=1:nPop
DSet=[];
DCount=0;
end
F{1}=[];
for i=1:nPop
for j=i+1:nPop
p=ResultM(:,:,i);
p1=Obj1(i);
p2=Obj2(i);
q=ResultM(:,:,j);
q1=Obj1(j);
q2=Obj2(j);
if Dominates(p1,q1,p2,q2)
DSet=[DSet j];
DCount=DCount+1;
end
end
end
I want to define the sets 'DSet' and 'DCount' for each i. I tried 'DSet(i)' and 'DCount(i)', but this doesn't work. Please help me in getting the right solution for this. Thanks in anticipation.
  2 件のコメント
Star Strider
Star Strider 2018 年 1 月 28 日
Please describe what you want to do and what your variables are (and their row and column dimensions).
MANISH KUMAR
MANISH KUMAR 2018 年 1 月 28 日
Sir, I want to define empty sets ([]); DSet and DCount for each individual value of i. For example, if i =1:5, then I need 5 different sets of DSet and DCount, so that as the loop runs from i=1 to i=5 and if statement is true, these sets can be updated and fetched separately for each i.

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

回答 (1 件)

Star Strider
Star Strider 2018 年 1 月 28 日
I still do not understand what you want to do. However, if you want to preallocate them, that is easy.
Delete these lines because you do not need the loop:
for i=1:nPop
DSet=[];
DCount=0;
end
and replace them with:
DSet = zeros(1,nPop);
DCount = 0;
and assuming that I understand what you want to do, the if block then becomes:
if Dominates(p1,q1,p2,q2)
DCount=DCount+1;
DSet(DCount) = j;
end
Experiment to get the result you want.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by