MATLAB: Acces data for find tsp distance by matrix indexing

linear index for multidimensional array

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

Best Answer

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.