2

我正在尝试编写一个程序来计算调用P一组数字 ( i=1:10) 的变量,除了一个数字 ( ind),每次都选择它。

第一次P是为计算的i=1:10,例如数字 4 被选为ind并使用,我们不希望它包含在下一次迭代中。因此P,必须计算下一次迭代i=[1:3 5:10]

我该如何处理?到目前为止,我所拥有的是:

for i=1:10

    i=1:i
        t = sum(Job(i,2))
    i=1:10
        P = mean(Job(i,2))
        Index= Job(i,4)/Job(i,2)*exp(-max(Job(i,1)-Job(i,2)-t,0)/2*P)
        X=max(Index)
        ind=find(Index >= X)
    completion_time(Job(ind,3))= machine_free_time + Job(ind,2)
    machine_free_time = completion_time(Job(ind,3))
    Lateness(Job(ind,3))= completion_time(Job(ind,3)) - Job(ind,1)
    Tardiness(Job(ind,3))= max(Lateness(Job(ind,3)),0)
end
4

3 回答 3

1

这样做的两种方法:

  1. 从向量中删除元素

    idcs = 1:10; % initially all values are included from 1 to 10
    for ii=1:10
        idx = choose one out of idcs
        % do your calculation
        % remove idx from idcs
        idcs(idcs==idx)=[];
    end
    
  2. 使用第二个向量来保存已使用的值:

    idcs = 1:10;
    valused=false(size(idcs));
    for ii=1:10
        idx = choose one out of idcs(~valused)
        % do your calculation
        % set the used value to true
        valused(idcs=idx)=true;
    end
    

因此,例如,将第二种方法用于您正在尝试做的事情,我认为它会像这样:

vector_i = 1:10
vector_i_used = false(size(vector_i));

for kk=1:10 % main loop
    P = calculateP( vector_i(~vector_i_used) );
    % ...
    ind = calculateInd(P, vector_i(~vector_i_used));
    vector_i_used(vector_i==ind) = true;
end
于 2012-12-10T17:44:37.937 回答
0

只需使用 if/then 语句跳过循环中所需的索引:

   skipThisIndex = [];
   for ii=1:10
      if ii~=skipThisIndex
         %  Do calculations
      end
      %Determine which index you want to skip next
      skipThisIndex = indexToSkipNext;
   end
于 2012-12-10T17:54:28.963 回答
0

我建议采用不同的方法。检查该值是否已包含在内,如果您已经测试过,则不要包含它。我将把代码和计算留给newDone你,但这种方法应该可行。

alreadyDone=false(10,1);
for i=1:10
   if (alreadyDone(i))
      %Do stuff here
   end
   alreadyDone(newDone)=true;
end
于 2012-12-10T17:43:46.220 回答