Question

I am running this program below and I keep getting this error "Index exceeds matrix dimensions" for line "Tour1 = pop(kk(1:10),:);". I don't know what I have missed out. Can anyone help please. Thanks

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
Was it helpful?

Solution

Just place the final end before executing the j block

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,:);

OTHER TIPS

Do you really want to have the second for inside the first one?

At the first execution of the outer loop, i is one. You populate pop (1, :), but no more.

At the inner loop, you then try to access pop (kk (1:10), :), with kk (1:10) being within [1..100]. But there is no pop (i, :) for i > 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top