function [P_extrinsic, P_mean, nimages] = calc_extrinsic_mean(prefix, postfix, ...
            sView, eView, incrView, imdim, lowdim)
P_mean = zeros([imdim imdim]);
nimages = 0;
for i=sView:incrView:eView,
   fname=[sprintf('%s%d%s',prefix, i,postfix)];
   A = load_matrix(fname)';
   if size(A,1) ~= imdim | size(A,2)~= lowdim,
       msg=[sprintf('The matrix loaded from %s has a dimension (%d,%d) that is not (%d,%d)',fname,size(A,1), size(A,2), imdim, lowdim)];
       disp(msg);
       msg=[sprintf('The matrix is skipped.')];
       disp(msg);
   else 
       nimages = nimages +1;
       P = A * ((A'*A)^(-1))*A';
       P_mean = P_mean + P;
   end
end
if nimages < 1,
  msg = [sprintf('There are not enough images to calculate extrinsic mean.')];
  disp(msg);
  return;
end
msg=[sprintf('%d low dimensional subspaces are loaded', nimages)];
disp(msg);
P_mean = P_mean / nimages;

[U S V]=svd(P_mean);

S = S*0;

S(1:lowdim, 1:lowdim) = eye(lowdim);

P_extrinsic = U * S * V'; 
return;
