-2

我在下面运行这个程序,我不断收到错误“索引超出矩阵尺寸”的行“Tour1 = pop(kk(1:10),:);”。我不知道我错过了什么。任何人都可以帮忙吗?谢谢

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
4

2 回答 2

2

end只需在执行j块之前放置final

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,:);
于 2013-10-09T10:15:03.277 回答
2

你真的想for在第一个里面有第二个吗?

在第一次执行外循环时,i是一个。你填充pop (1, :),但没有更多。

在内部循环中,您然后尝试访问pop (kk (1:10), :)kk (1:10)并且在 [1..100] 内。但是pop (i, :)对于 i > 1 没有。

于 2013-10-09T10:17:12.970 回答