How to find the corresponding vector?

조회 수: 4 (최근 30일)
vinicius lanziotti
vinicius lanziotti 2017년 12월 8일
편집: James Tursa 2017년 12월 8일
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];
Dist = zeros(1,n);
for k=1:n
xp = randperm(numel(x), 2);
x(xp) = x(fliplr(xp)) % vector x permuted in two positions
s = sub2ind(size(d),x(1:end-1),x(2:end ));
Dist(k) = sum(d(s));
Distance = Dist(1,k) % travaled distance by elements of the vector x
end
Lowest_Distance = min(Dist)
I need to find the vector x corresponding to Lowest_Distance.
For exemple:
>>
Lowest_Distance = 170
x=
1 2 3 5 4

채택된 답변

James Tursa
James Tursa 2017년 12월 8일
편집: James Tursa 2017년 12월 8일
Dist = zeros(1,n);
Distx = zeros(n,numel(x)); % <-- ADDED, allocate for saved x vectors
for k=1:n
xp = randperm(numel(x), 2);
x(xp) = x(fliplr(xp)) % vector x permuted in two positions
s = sub2ind(size(d),x(1:end-1),x(2:end ));
Dist(k) = sum(d(s));
Distx(k,:) = x; % <-- ADDED, save the x vector for this iteration
Distance = Dist(1,k) % travaled distance by elements of the vector x
end
[Lowest_Distance,ix] = min(Dist); % <-- MODIFIED, get the index of the min
Lowest_Distancex = Distx(ix,:); % <-- ADDED, pick off x vector corresponding to the min

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Digital Filter Analysis에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by