function [percent,C,ave_planes] = cross_val_g(A,B,Agroups,Bgroups,maxdepth,tol,CF,min_points)
% CROSS_VAL_G	does the cross validation for MSMT for the given ordered
%		sets A,B and Agroups, Bgroups
%
%	[percent,C,ave_surf] = cross_val_g(A,B,Agroups,Bgroups,maxdepth,tol,CF,min_points)	

global n;

if nargin < 8
	min_points = 0;
end
if nargin < 7
	CF = 0.0;
end
if nargin < 6
	tol = 0.0;
end
if nargin < 5 
	maxdepth = round(realmax);
end
n = size(A,2);
totmissA = 0;
totmissB = 0;
totcorrectA = 0;
totcorrectB = 0;
numplanes = 0;
% print out a header
disp(['Count' blanks(2) 'Total Correct' blanks(2) 'Total Incorrect' blanks(2) 'Percentage Correct'])

for i = 1:10

	[Atest,Btest,Atrain,Btrain] = cross_sets(A,B,Agroups,Bgroups,i);

% Train! (grow the Tree for these training points)

	T = [];
	T = msmt_tree(Atrain,Btrain,maxdepth,tol,CF,min_points);
	numplanes = numplanes + size(T,2); % number of planes is the 
					      % number of columns in Tree

% test accuracy on training set!:
%	[mtrainA,ctrainA,mtrainB,ctrainB] = cross_classify(T,Atrain,Btrain);

%	disp([ sprintf('MisTrainA: %2d',mtrainA) ' '  sprintf('CorTrainA: %2d',ctrainA) ' ' sprintf('misTrainB: %2d',mtrainB) ' ' sprintf('corTrainB: %2d',ctrainB)])

% Test! (test with the test points)

	[missA,correctA,missB,correctB] = cross_classify(T,Atest,Btest);
	totmissA = totmissA + missA;
	totmissB = totmissB + missB;
	totcorrectA = totcorrectA + correctA;
	totcorrectB = totcorrectB + correctB;



% Print out some information regarding this test:
	disp([ num2str(i) blanks(7) sprintf('%7.3f ',correctA + correctB) blanks(6) sprintf('%7.3f ',missA + missB) blanks(6) sprintf('%7.3f ', (correctA + correctB)/(correctA + correctB + missA + missB))])
		
	Atest = A(Agroups(i):Agroups(i+1)-1,:);
	Btest = B(Bgroups(i):Bgroups(i+1)-1,:);
	Atrain = [ A(1:Agroups(i)-1,:);A(Agroups(i+1):size(A,1),:)];
	Btrain = [ B(1:Bgroups(i)-1,:);B(Bgroups(i+1):size(B,1),:)];
end % for

% compute percentage correctly classified:

percent = ((totcorrectA + totcorrectB)/(size(A,1)+size(B,1)))*100;
C = [ totcorrectA, totmissB;
      totmissA, totcorrectB ];
ave_planes = numplanes/10;


