function U = energy(M,Y)
% ENERGY Calculate energy of a configuration for a graphical model
%
% U = ENERGY(M,Y)

  ndx = sub2ind( size(M.nodeEnergies), Y, 1:size(M.adjmat,1));
  
  U = sum( M.nodeEnergies(ndx) );
  
  [N0,N1] = find(M.adjmat);
  
  for e=1:length(N0)
	
	n0 = N0(e);
	n1 = N1(e);
	
	U = U + M.edgeEnergies{n0,n1}( Y(n0), Y(n1) );
	
  end;
 
