In the 3rd for loop , xloc(k) and yloc(k) are both vectors of length 6. And xp and yp are vectors of length 30 . If done alone the subtraction returns an error but here it doesn't. I wanna know what's so special here that it doesn't return an error?
if true % xloc = [1,7,8,17,22,27] ;
yloc = [28,18,16,2,10,8] ;V = [3,7,4,5,2,6] ;best_loc_x = 31 ;best_loc_y = 31 ;min_cost = 1e+6 ;anotherx = [] ;anothery = [] ;C = [] ;for xp = 1:30 for yp = 1:30 for k = 1:6 d(k) = sqrt((xloc(k)-xp).^2+(yloc(k)-yp).^2) ; cost(k) = d(k)*V(k); end loc_cost(xp,yp) = sum(cost) ; if loc_cost(xp,yp) < min_cost best_loc_x = xp; best_loc_y = yp; min_cost = loc_cost(xp,yp); elseif (loc_cost(xp,yp)-min_cost) <=1 anotherx = [anotherx,xp] ; anothery = [anothery,yp]; C = [C,loc_cost(xp,yp)]; end endendend
Best Answer