matthiasm@8: function plot_matrix(G, bw) matthiasm@8: % PLOT_MATRIX Plot a 2D matrix as a grayscale image, and label the axes matthiasm@8: % matthiasm@8: % plot_matrix(M) matthiasm@8: % matthiasm@8: % For 0/1 matrices (eg. adjacency matrices), use matthiasm@8: % plot_matrix(M,1) matthiasm@8: matthiasm@8: if nargin < 2, bw = 0; end matthiasm@8: matthiasm@8: if 0 matthiasm@8: imagesc(G) matthiasm@8: %image(G) matthiasm@8: %colormap([1 1 1; 0 0 0]); % black squares on white background matthiasm@8: %colormap(gray) matthiasm@8: grid on matthiasm@8: n = length(G); matthiasm@8: matthiasm@8: % shift the grid lines so they don't intersect the squares matthiasm@8: set(gca,'xtick',1.5:1:n); matthiasm@8: set(gca,'ytick',1.5:1:n); matthiasm@8: matthiasm@8: % Turn off the confusing labels, which are fractional matthiasm@8: % Ideally we could shift the labels to lie between the axis lines... matthiasm@8: % set(gca,'xticklabel', []); matthiasm@8: % set(gca,'yticklabel', []); matthiasm@8: else matthiasm@8: % solution provided by Jordan Rosenthal matthiasm@8: % You can plot the grid lines manually: matthiasm@8: % This uses the trick that a point with a value nan does not get plotted. matthiasm@8: imagesc(G); matthiasm@8: if bw matthiasm@8: colormap([1 1 1; 0 0 0]); matthiasm@8: end matthiasm@8: n = length(G); matthiasm@8: x = 1.5:1:n; matthiasm@8: x = [ x; x; repmat(nan,1,n-1) ]; matthiasm@8: y = [ 0.5 n+0.5 nan ].'; matthiasm@8: y = repmat(y,1,n-1); matthiasm@8: x = x(:); matthiasm@8: y = y(:); matthiasm@8: line(x,y,'linestyle',':','color','k'); matthiasm@8: line(y,x,'linestyle',':','color','k'); matthiasm@8: set(gca,'xtick',1:n) matthiasm@8: set(gca,'ytick',1:n) matthiasm@8: end matthiasm@8: matthiasm@8: