Jam1
Jam1

Reputation: 649

Put a numbers from a (n by n) matrix on each cell of a grid

I have implemented the A* algorithm to solve the 8-puzzle. However I want to be more fancy by displaying the result of each state change of my 3 by 3 matrix on a grid that animates the state.

My matrix has numbers from 0 to 8, so I want a grid with 3 rows and 3 columns with a number on each tile.

I really do not know where to start, all ideas are welcome.

Below the first matrix is where I have started, and I used A* to reach the last state which is the goal state. I would like to display these matrix on a grid, and show the transitions graphically. So each time the matrix changes, the grid also will change.

 2     8     3
 1     6     4
 7     0     5

 2     8     3
 1     0     4
 7     6     5

 2     0     3
 1     8     4
 7     6     5

 0     2     3
 1     8     4
 7     6     5

 1     2     3
 0     8     4
 7     6     5

 1     2     3
 8     0     4
 7     6     5

Upvotes: 1

Views: 79

Answers (1)

EBH
EBH

Reputation: 10440

Have a look at this for example of a use of imagesc:

P = perms(0:8);
A = reshape(P(1:100,:).',3,[]);
A = reshape(A,3,3,[]);

for k = 1:size(A,3)
    imagesc(A(:,:,k))
    axis off
    pause(0.1)
end

opt_1

if you want to add the borders, you can either pad it with nans:

B = nan(5,5,size(A,3));
B(1:2:5,1:2:5,:) = A;
cmap = colormap;
cmap(1,:) = [0 0 0];
colormap(cmap)

for k = 1:size(B,3)
    imagesc(B(:,:,k))
    axis off
    pause(0.1)
end

opt_2

or use pcolor (with some padding with nans):

B = nan(4,4,size(A,3));
B(1:3,1:3,:) = A;

for k = 1:size(B,3)
    pcolor(B(:,:,k))
    axis off
    pause(0.1)
end

opt_3

Upvotes: 1

Related Questions