Reputation: 305
I have a matrix which shows me the posiions of a knight in a knights tour. I'm looking to find a way of firstly finding the numbers in order and outputting their positions e.g. on a smaller board X
X=[1 3; 4 2]
OUTPUT
A=[1 2 3 4]
b= [1 1; 2 4; 1 2; 1 3]
Something like this where b is the position of the values of A in the matrix
the only way I can think about doing this is using a series function of find (n)
where n=1..64
and then concatenating results
I then want to use this information to create a plot of the moves with a line/vector plot but am having difficulty in working out how to do this also.
Thanks, Tessa
Upvotes: 3
Views: 895
Reputation: 74940
You can use find
to identify the visited board coordinates, followed by sorting them according to the order of the moves.
%# find the visited coordinates
[rows,cols,moveNumber]=find(A);
%# find out how to reorder the positions so that
%# the moves are in the right order
[~,sortIdx] = sort(moveNumber);
%# plot the moves
figure
plot(rows(sortIdx),cols(sortIdx),'-o')
Upvotes: 3