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
end
end
end

6 comentarios

dpb
dpb el 27 de Mayo de 2019
You didn't attach the relevant code snippets...
Uzair Khan
Uzair Khan el 27 de Mayo de 2019
Check it now
dpb
dpb el 27 de Mayo de 2019
Editada: dpb el 27 de Mayo de 2019
Post the text we can read (formatted as code), not images...can't see all of that in one place.
Uzair Khan
Uzair Khan el 27 de Mayo de 2019
Please don't mind my inefficiency, i am new here.
dpb
dpb el 27 de Mayo de 2019
Editada: dpb el 27 de Mayo de 2019
'Tis OK, but it makes more likely you'll get responses when make it easier for the folks to tryto help... :)
Do you see what's going on now w/ the Answer to help?
Uzair Khan
Uzair Khan el 27 de Mayo de 2019
No haven't really spent much time. So what about this question?

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 27 de Mayo de 2019

0 votos

for xp = 1:30 %so xp is a scalar
for yp = 1:30 %so yp is a scalar
for k = 1:6 %so k is a scalar
d(k) = sqrt((xloc(k)-xp).^2+(yloc(k)-yp).^2) ;
vector indexed at a scalar gives a scalar, so xloc(k) is a scalar and yloc(k) is a scalar. You subtract the scalars xp and yp from them, giving scalars for the sub-expressions. You square, add, square-root, giving a scalar output, which you store in a scalar location. No problem.
for xp = 1:30
does not make xp a vector of 30 elements: it tells MATLAB to iterate making xp hold each of the values 1:30 in turn, so at any one time xp is a scalar.

4 comentarios

Uzair Khan
Uzair Khan el 27 de Mayo de 2019
Thanks. U cleared the for loop too for me
Uzair Khan
Uzair Khan el 28 de Mayo de 2019
Just one more question. Doesn't d(k) mean the first k number of calculations of d? If it does then we wouldn't find the distance for the rest of the 24 iterations, or would we? In other words please explain how d(k) works.
Walter Roberson
Walter Roberson el 28 de Mayo de 2019
If you start from the beginning of a vector and count 1 for the first element, 2 for the second element, and so on, then d(k) refers to the content of the element for which the count is the same as the value k. For example d(7) is the seventh element. It is not the first seven elements together, just the one.
Uzair Khan
Uzair Khan el 28 de Mayo de 2019
Ok thanks

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 27 de Mayo de 2019

0 votos

The key is line 17...

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 27 de Mayo de 2019

Comentada:

el 28 de Mayo de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by