Asked by Yogesh Shukla
on 18 Apr 2013

for p = 4:4:population routes = pop(rand_pair(p-3:p),:); dists = total_dist(rand_pair(p-3:p)); [~,idx] = min(dists); best_of_4_route = routes(idx,:); ins_pts = sort(ceil(n*rand(1,2))); I = ins_pts(1); J = ins_pts(2); for k = 1:4 tmp_pop(k,:) = best_of_4_route; switch k case 2 tmp_pop(k,I:J) = fliplr(tmp_pop(k,I:J)); case 3 tmp_pop(k,[I J]) = tmp_pop(k,[J I]); case 4 tmp_pop(k,I:J) = tmp_pop(k,[I+1:J I]); otherwise end display(best_of_4_route); display(d);

% diary on a_str = sprintf('%e\t',d); fid = fopen('newfile.txt','w'); fprintf(fid, '%s', a_str); fclose(fid);

new_pop(p-3:p,:) = tmp_pop; end

I want to store value of 'best_of_4_route', 'd' for every iteration. The code I have written only overwrite.

*No products are associated with this question.*

Answer by Mahdi
on 18 Apr 2013

This is only one of **many** ways (I would suggest pre-locating but I need more of the code) *Before *the for loop, add this line:

best_of_4_route=[];

And change your the line inside the for loop to

best_of_4_route =[best_of_4_route; routes(idx,:)];

Similar logic for d.

Show 1 older comment

Yogesh Shukla
on 18 Apr 2013

for p = 4:4:population routes= pop(rand_pair(p-3:p),:);

dists = total_dist(rand_pair(p-3:p));

[~,idx] = min(dists); best_of_4_route = routes(idx,:); ins_pts = sort(ceil(n*rand(1,2))); I = ins_pts(1); J = ins_pts(2);

best=[];

for k = 1:4 tmp_pop(k,:) = best_of_4_route; switch k case 2 tmp_pop(k,I:J) = fliplr(tmp_pop(k,I:J)); case 3 tmp_pop(k,[I J]) = tmp_pop(k,[J I]); case 4 tmp_pop(k,I:J) = tmp_pop(k,[I+1:J I]); otherwise

end display(best_of_4_route); display(d); best=[best_of_4_route; routes(idx,:)];

end

Its not working. I don't know the correct way to do that.

Mahdi
on 18 Apr 2013

I meant the first for loop. Try this:

best_of_4_route=[] for p = 4:4:population routes = pop(rand_pair(p-3:p),:); dists = total_dist(rand_pair(p-3:p)); [~,idx] = min(dists); best_of_4_route = [best_of_4_route; routes(idx,:)]; ins_pts = sort(ceil(n*rand(1,2))); I = ins_pts(1); J = ins_pts(2); for k = 1:4 tmp_pop(k,:) = best_of_4_route; switch k case 2 tmp_pop(k,I:J) = fliplr(tmp_pop(k,I:J)); case 3 tmp_pop(k,[I J]) = tmp_pop(k,[J I]); case 4 tmp_pop(k,I:J) = tmp_pop(k,[I+1:J I]); otherwise end display(best_of_4_route); display(d);

% diary on a_str = sprintf('%e\t',d); fid = fopen('newfile.txt','w'); fprintf(fid, '%s', a_str); fclose(fid);

new_pop(p-3:p,:) = tmp_pop; end end

Yogesh Shukla
on 19 Apr 2013

Its not working.. only top 6 nodes are stored of each route, and overwritten again in 'newfile.txt'.

best = [best; routes(idx,:)]; save('d') a_str = sprintf('%d\t',best); fid = fopen('newfile.txt','w'); fprintf(fid, '%s', a_str); fclose(fid);

However 50x77 size matrix stores 77 routes when i save workspace by using save('d'). But i need to use it in txt file.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments