How to find the lowest distance?

1 visualizzazione (ultimi 30 giorni)
x = [1 2 3 4 5];
d = [0 10 20 30 40
10 0 50 60 70
20 50 0 80 90
30 60 80 0 100
40 70 90 100 0];
for k=1:n
xp = randperm(numel(x), 2);
x(xp) = x(fliplr(xp))
s = sub2ind(size(d),x(1:end-1),x(2:end ));
Distance = sum(d(s))
end
Lowest_Distance = ?

Risposta accettata

Guillaume
Guillaume il 8 Dic 2017
Using words to explain what you want would be useful, rather than leaving it up to the reader to guess.
Also using comments in your code would be useful to explain what it is doing (the big picture).
A guess, that you want the minimum of the Distance values calculated at each step of your k loop. In that case, simply store the values into an array and take the minimum:
Distance = zeros(1, n);
for k = 1:n
...
Distance(k) = sum(d(s));
end
min_Distance = min(Distance)

Più risposte (0)

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by