for循环中的索引错误,MATLAB

时间:2013-10-09 09:59:59

标签: matlab matrix evolutionary-algorithm

我在下面运行这个程序,我一直收到“Index1 = 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

2 个答案:

答案 0 :(得分: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,:);

答案 1 :(得分:2)

你真的想在第一个for内有第二个i吗?

在第一次执行外循环时,pop (1, :)就是一个。您填充pop (kk (1:10), :),但不再填充。

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