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