2013-10-09 82 views
-2

我運行下面這個程序,我不斷收到此錯誤「指數超過矩陣尺寸」線路「Tour1 =彈出(KK(1:10),:);」。我不知道我錯過了。任何人都可以請幫忙。 感謝指數誤差在for循環,MATLAB

for i = 1:100        %Population Initialization 
    pop(i,1) = 50 - rand*(50-1); 
    pop(i,2) = 1 - rand*(1-0.1); 
    pop(i,3) = 0.2 - rand*(0.2-0.01); 
    Kc(i) = pop(i,1); 
    T1(i) = pop(i,2); 
    T2(i) = pop(i,3); 

    fitness(i) = obj(Kc(i), T1(i), T2(i)); 

%% TOURNAMENT SELECTION 

for j = 1:10         %Select first parent 
    kk = randperm(100); 
    Tour1 = pop(kk(1:10),:); 
    temp1 = mat2cell(Tour1(j,:),1,ones(1,numel(Tour1(j,:)))); 
    ZET1(j) = obj(temp1{:}); 
end 

[Val1 Ind1] = max(ZET1); 
parent1 = Tour1(Ind1,:); 

end 
+0

在Tour1中訪問它之前,您需要使用tofinish填充'pop'。使用debug:如果error''>> DBSTOP,看看'kk'和'pop'是 – Shai

回答

2

只需將最終end執行j

for i = 1:100        %Population Initialization 
    pop(i,1) = 50 - rand*(50-1); 
    pop(i,2) = 1 - rand*(1-0.1); 
    pop(i,3) = 0.2 - rand*(0.2-0.01); 
    Kc(i) = pop(i,1); 
    T1(i) = pop(i,2); 
    T2(i) = pop(i,3); 
    fitness(i) = obj(Kc(i), T1(i), T2(i)); 
end 

for j = 1:10         %Select first parent 
    kk = randperm(100); 
    Tour1 = pop(kk(1:10),:); 
    temp1 = mat2cell(Tour1(j,:),1,ones(1,numel(Tour1(j,:)))); 
    ZET1(j) = obj(temp1{:}); 
end 

[Val1 Ind1] = max(ZET1); 
parent1 = Tour1(Ind1,:); 
+0

謝謝你們,錯誤已經消失。 – dee

2

之前你真的想擁有的第一個內部的第二for

在外環的第一次執行,i是一個。您填寫pop (1, :),但沒有更多。

在內循環中,您嘗試訪問pop (kk (1:10), :),其中kk (1:10)在[1..100]之內。但沒有pop (i, :) i> 1.