

function [muhat, treat, MSTr, MSE, F] = fanova(mat, groupss)
% This function does F(rows)ANOVA(columns).

si=size(mat);
n=si(1); N=si(2);
p = length(groupss);
 if (sum(groupss) ~= n) 
    error('Please check the treatment sample sizes.')
 end

 muhat=[]; treat=[]; F=[]; MSTr=[]; MSE=[]; 

 for j = 1:N
   coldat = mat(:,j);
   bardd = mean(coldat);
   bardds=repmat(bardd,1,n);
   grmeans=[]; grmean=[];
   mark=0; ind=1;
   while (ind <= p)
      lb=mark + 1; ub=mark+groupss(ind);
      gm = mean( coldat( lb:ub ));
      grmeans=[grmeans, repmat(gm, 1, groupss(ind)) ];
      grmean=[grmean, gm];
      mark=mark + groupss(ind);
      ind=ind+1;
   end
   muh = bardd; alphah = grmean - muh;
   sst=0.00001; sstr=0.00001; sse=0.00001;

   for  i = 1:n
      sst = sst + (coldat(i)-bardd)^2;
      sstr = sstr + (grmeans(i) - bardd)^2;
      sse =  sse + ( coldat(i) - grmeans(i) )^2;
   end
   mstr=sstr/(p-1);
   mse = sse/(n-p); 
   f = mstr/mse;
   MSTr=[MSTr, mstr]; MSE=[MSE, mse]; F=[F,f];
   muhat=[muhat, muh]; 
   treat=[treat, alphah'];
end

%
