function [M, piece] = adjacency(piece) %Computes the upper triangular part of the adjacency matrix of a 2-d binary %array, and numbers the squares. warning off MATLAB:conversionToLogical piece = int8(logical(piece)); warning on MATLAB:conversionToLogical count = 0; [l, w] = size(piece); %Number the nodes for i = 1:l for j = 1:w if piece(i, j) count = count + 1; piece(i, j) = count; end end end M = logical(zeros(count)); for i = 1:(l - 1) for j = 1:(w - 1) c = piece(i, j); if c d = piece(i + 1, j); if d M(c, d) = 1; end r = piece(i, j + 1); if r M(c, r) = 1; end end end c = piece(i, w); if c d = piece(i + 1, w); if d M(c, d) = 1; end end end for j = 1:(w - 1) c = piece(l, j); if c r = piece(l, j + 1); if r M(c, r) = 1; end end end