how to calculate the indices's distance of a vector using the data of a matrix?
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Honglei Chen
on 7 Dec 2017
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
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!