function [ext_dist] = calc_pairwise_dist_proj(A1, A2, lowdim, P1); 

len = round(size(A1,2)/lowdim);
dist = (1:len)*0.0;
imdim = size(A1,1); 
j = mod(0+len,len)+1;

A21 = A2(:,((j-1)*lowdim+1):(j*lowdim));
P21 =  A21 * ((A21'*A21)^(-1))*A21';

for j=0:(len-1),
  dist1 = 0.0;
  k1 = mod(j+1,len)+1;
  A21 = A2(:,((k1-1)*lowdim+1):(k1*lowdim));
  P22 =  A21 * ((A21'*A21)^(-1))*A21';
  for i=1:len,
    i1 = mod(i,len)+1;
    k = mod(j+1-i+len,len)+1;
    dist(k) = dist(k) + norm( (P1(:,((i1-1)*imdim+1):(i1*imdim))-P1(:,((i-1)*imdim+1):(i*imdim))) - (P22-P21),'fro');
  end
  P21 = P22;
  
end
dist = sqrt(dist/len);
%dist
ext_dist = min(min(dist));
[I J] = find(dist ==ext_dist);
if J ~= 1,
 disp([sprintf('The minimum angle is %d',J)]);
end

return;
% keyboard;
    






