Re: Understanding Array indexing and the find command



Dear Tim

I am not sure that you use the correct way to calculate the index vector ti.
Is nn the dimension of X or DM ?
Remember that Matlab store matrice DM columwise. (For a more complete
explanation see the help documentation
at "Programming and Data Types: M-File Programming: Advanced Indexing")

I would rather use same code like:
% DM=[ n x n ] matrice storing cities distance, DM(a,b)=d indicate that from
a to b distance is d
% diagonal of DM are zero, DM may not necessary symetric (distance from a to
b not necessery egual to distance from b to a

C =[ 2 1 3 4 ] % city list
CC=circshift(C,[1 -1]); % equivalent to shift C rows to
the left (or [ C(2:end) C(1) ] )
ti = C + (CC-1) * size(DM,1); % see DOC for description of array (or
matrice) indexing

s = sum(DM(ti)); % return total length of C
trip
m = max(DM(ti)); % return max length leg in
trip C
i = find(DM(ti) == m); % return position(s) of max
length leg in trip ti
[C(i); CC(i)] % return city number of
leg(s) of max length from (row1) to (row2)

Hiope this help you, or at least give you same idea to continue.

C.Ret




"Tim Booher" <timothy.booher@xxxxxxxx> wrote
innews:ef28c6f.-1@xxxxxxxxxxxxxxxxxxx
I am in a coding quandary that I can't seem to figure out.

I am trying to solve the traveling salesman problem with the tabu
heuristic.

Given a list of cities, X, say, [2 1 3 4], I calculate the indicies
of the distance matrix, ti, as,

ti = X + [X(2:nn) X(1)]*nn+1;

so i can get the total tour distance from the distance matrix, DM, by
simply:

sum((DM(ti))

or the maximum distance between two stops as,

max(DM(ti))

Now, the hard part is I want to get the x and y indicies back from
the distance matrix.

I came up with the following solution, but don't think it is very
elegant.

for i = 1:length(ti)
if(ti(i)==MDM)
break;
end
end
X = mod(ti(i)-1,nn)+1
Y = ceil(ti(i)/nn)]

where X and Y are the indices of the distance matrix that correspond
to the maximum length arc in the current tour

i workd on this for about 10 hours with different modifications of
the find command, etc and this is the best I can get, but I have this
feeling that i am making life _much_ too complicated (and
consequently slow) with mod and ceiling operators.

any thoughts by those more aware of matlab techniques would be
greatly appreciated.


.



Relevant Pages

  • Re: Distributing n points in space
    ... distance matrix por each pair of points distance from one to the ... The n x n positive semidefinite matrix G ... negative eigenvalues or rank> k, the best approximation, in some sense, ... Now given the distance matrix D: ...
    (sci.math)
  • Re: Finding similar entries
    ... then you don't need the interpoint distance ... the huge interpoint distance matrix. ... also for free at least in SOM toolbox ...
    (comp.soft-sys.matlab)
  • Re: fast curve similarity needed
    ... For a cuve defined by N points, the distance matrix would be the NxN array of all the pairwise distances. ... The distance matrix is invariant under rotations of the curve, and by normalizing the distances you can make it invariant under uniform scaling as well. ... The functions should have the option to match curves regardless of rotation in 3D space, and be preferably invariant in terms of uniform scale as well. ...
    (comp.graphics.algorithms)
  • Re: Looking for "diff" algorithm.
    ... > I'm looking for an implementation of a difference algorithm. ... Google for "minimal edit distance". ... where lev_matrix calculates the distance matrix. ...
    (comp.lang.prolog)
  • Re: Garmin GPS 12 - TRIP accuracy?
    ... Today's walk, shown in both Memory-Map and GPS Utility as 15.8 miles, ... The trip odometer probably accumulates distance with its own filter, ... significantly lower than the 76S and lower than its own track log. ...
    (sci.geo.satellite-nav)