Re: Understanding Array indexing and the find command
- From: "CRet" <c.ret@xxxxxxxxxx>
- Date: Tue, 14 Feb 2006 11:36:42 +0100
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.
.
- References:
- Understanding Array indexing and the find command
- From: Tim Booher
- Understanding Array indexing and the find command
- Prev by Date: repetetive runs => matlab slows down
- Next by Date: colorbar postscript problem
- Previous by thread: Understanding Array indexing and the find command
- Next by thread: Fibril Orientation Measurement
- Index(es):
Relevant Pages
|