how to calculate the indices's distance of a vector using the data of a matrix?
2 ビュー (過去 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.
0 件のコメント
回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!