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