function [A,nc,nn] = accuracy(C,Y,X,P)
% ACCURACY Node-level classification accuracy from MAP prediction
%
% [A,N,D] = ACCURACY(C,Y,X) where C is the GCRF object, Y is a cell
% of class labels and X the array of "grid graph feature"
% structs. A is the fraction of correctly labeled nodes using MAP
% prediction, the result of N/D.
%
% Note: This function is not designed to handle NaN labelings.  

% Jerod Weinman
% jerod@acm.org
    
% $Id: accuracy.m 1 2009-05-07 13:41:00Z weinman $
 
% Jerod Weinman
% jerod@acm.org
% Copyright 2004, 2008, 2009
 
%    This file is part of Matlab GridCRF.
%
%    Matlab GridCRF 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.
%
%    Matlab GridCRF 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 Matlab GridCRF.  If not, see <http://www.gnu.org/licenses/>.
  
  nc = 0;
  nn = 0;
  
  for a=1:length(Y)
	P = map(C,X(a));
	
	nc = nc + sum(P==mkcvector(Y{a}));
	nn = nn + length(Y{a});
  end;
  
  A = nc/nn;
