function u=avp(v,p); %averages every p rows of v into 1.

if (p==1)|(size(v,1)==1)
   u=v;
else


   [m,n]=size(v);

   mm=floor(m/p)*p;
   
   if (m-mm)>0%for averaging remainder of matrix if there is a remainder
      u2=v(mm+1:m,:);
      v(mm+1:m,:)=[];
      if size(u2,1)>1
         u2=mean(u2);
      end
   end
   %averaging main portion of matrix
   
   v=reshape(v,p,mm/p,n);
   u=squeeze(mean(v));
   
   if (n~=1) & (size(u,2)==1)
      u=u';
   end
   
   
   if n==1%guarantee that the output for column vector is a column vector
      u=u(:);
   end

   if (m-mm)>0
      u(size(u,1)+1,:)=u2;
   end
   clear u2
   


end
