how to calculate the indices's distance of a vector using the data of a matrix?

3 次查看(过去 30 天)
something like this:
% Distance Matrix:
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];
% Positions Vector:
x = [1 2 3 4 5];
I want the distances traveled from one element to the other of the vector, using the indices d (i, j) of the distance matrix.
for exemple: x = [1 2 3 4 5];
1->2 it's equal the distance of element d(1,2) or d(2,1) of distance matrix.
2->3 it's equal the distance of element d(2,3) or d(3,2) of distance matrix.
In the end, returns the traveled distance of the vector x = [1 2 3 4 5];
So:
distance = (1->2) + (2->3) + (3->4) + (4->5)
PS. THE VECTOR IS GENERIC.

回答(1 个)

Honglei Chen
Honglei Chen 2017-12-7
Not sure if I missed anything but looks to me this can be done by
sum(d(sub2ind(size(d),x(1:end-1),x(2:end))))
HTH

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by