How to define variable sets for changing value of i ?

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by