Skip to content

Commit

Permalink
add lattice
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Mar 30, 2017
1 parent fa8d013 commit 6de9257
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions common/lattice.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function A = lattice( sz )
% Create an undirected graph corresponding to sz lattice
% Example:
% plot(graph(lattice([2,2,3])))
% Input:
% sz: 1 x d size of lattice
% Output:
% A: prod(sz) x prod(sz) adjacent matrix of an undirected graph
% Written by Mo Chen ([email protected])
d = numel(sz);
step = cumprod(sz);
n = step(end);
M = reshape(1:n,sz);
S = arrayfun(@(i) reshape(slice(M,i,1:sz(i)-1),1,[]), 1:d,'UniformOutput',false);
T = arrayfun(@(i) reshape(slice(M,i,2:sz(i)),1,[]), 1:d,'UniformOutput',false);
A = sparse([S{:}],[T{:}],1,n,n);
A = A+A';

0 comments on commit 6de9257

Please sign in to comment.