필터 지우기
필터 지우기

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.

답변 (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에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by