function P = mpm(G,M)
% MPM Calculate maximum posterior marginal configuration
%
% P = MPM(G) uses LOOPYBP to calculate the node marginals,
% returning the label indices for each node in P.
%
% P = MPM(G,M) where M are the node marginals to use.
%
% See also LOOPYBP, SPLOOPYBP, ASYNCBP

% Jerod Weinman
% jerod@acm.org

% $Id: mpm.m 34 2011-07-28 17:46:52Z weinman $
 
% Jerod Weinman
% jerod@acm.org
% Copyright 2008, 2009, 2011
 
%    This file is part of MatLibFactorGraph.
%
%    MatLibFactorGraph is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    MatLibFactorGraph is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with MatLibFactorGraph.  If not, see <http://www.gnu.org/licenses/>.

  if nargin<2
    [B,M] = loopybp(G);
  elseif length(M)~=G.numNodes
    error('Length of variable marginals does not match number of variables');
  end;

  P = cellfun(@(b) argmax(b),M);
