acces data for find tsp distance by matrix indexing

조회 수: 1 (최근 30일)
msr16
msr16 2019년 1월 28일
댓글: msr16 2019년 1월 28일
hi, i am have a distance matrix A=[0 2 5; 5 0 4; 6 3 0] and the route is in another matrix B=[1 2 3; 1 3 2; 2 1 3; 2 3 1]
by using the B i need to find the distance which is in A
Eg:
Taken the1st row of B [1 2 3] it means 1st row and 2nd column of A value taken (2) then 2nd row and 3rd column of A value taken(4) for each row of B wil get the value from A and store in another matrix C[2 4]
need code for this...
Thanks in Advance
  댓글 수: 1
Stephen23
Stephen23 2019년 1월 28일
Your question has nothing to do with cell arrays. MATLAB's basic data classes (e.g. cell arrays and numeric arrays) are explained in the MATLAB documentation:

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 1월 28일
편집: Walter Roberson 2019년 1월 28일
sum(A(sub2ind(size(A), B(:,1:end-1), B(:,2:end))),2)
By the way your question has nothing to do with cell arrays.
  댓글 수: 4
Walter Roberson
Walter Roberson 2019년 1월 28일
You can extend the
sub2ind(size(A), B(:,1:end-1), B(:,2:end))
to have as many dimensions as you need. The first input is the array size; the second corresponds to the first index; the third corresponds to the second index. You can keep going with more indices. The N'th output element of
idx = sub2ind(size(A), P, Q, R)
would correspond to the linear index in A of A(P(N), Q(N), R(N)) such that A(idx(N)) would be the same as A(P(N), Q(N), R(N))
msr16
msr16 2019년 1월 28일
Thankyou

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by