function [Outputmatrix2, Outputmatrix] = averageEye_V3vel(Inputmatrix)
% Inputmatrix is a n x 2  matrix with n=1: eyevelocity and n=2: DF/F
% Outputmatrix2 is a 30 x 1 table with median of all 30 columns
% Outputmatrix is raw data
% V3 binnes data in 0.5°/frame
Outputmatrix=nan(size(Inputmatrix,1),20);

for t=1:size(Inputmatrix,1)
    if Inputmatrix(t,1)>=-5 && Inputmatrix(t,1)<-4.5
       Outputmatrix(t,1)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-4.5 && Inputmatrix(t,1)<-4
       Outputmatrix(t,2)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-4 && Inputmatrix(t,1)<-3.5
       Outputmatrix(t,3)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-3.5 && Inputmatrix(t,1)<-3
       Outputmatrix(t,4)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-3 && Inputmatrix(t,1)<-2.5
       Outputmatrix(t,5)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-2.5 && Inputmatrix(t,1)<-2
       Outputmatrix(t,6)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-2 && Inputmatrix(t,1)<-1.5
       Outputmatrix(t,7)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-1.5 && Inputmatrix(t,1)<-1
       Outputmatrix(t,8)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-1 && Inputmatrix(t,1)<-0.5
       Outputmatrix(t,9)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-0.5 && Inputmatrix(t,1)<0
       Outputmatrix(t,10)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=0 && Inputmatrix(t,1)<0.5
       Outputmatrix(t,11)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=0.5 && Inputmatrix(t,1)<1
       Outputmatrix(t,12)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=1 && Inputmatrix(t,1)<1.5
       Outputmatrix(t,13)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=1.5 && Inputmatrix(t,1)<2
       Outputmatrix(t,14)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=2 && Inputmatrix(t,1)<2.5
       Outputmatrix(t,15)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=2.5 && Inputmatrix(t,1)<3
       Outputmatrix(t,16)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=3 && Inputmatrix(t,1)<3.5
       Outputmatrix(t,17)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=3.5 && Inputmatrix(t,1)<4
       Outputmatrix(t,18)=Inputmatrix(t,2);
    end    
    if Inputmatrix(t,1)>=4 && Inputmatrix(t,1)<4.5
       Outputmatrix(t,19)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=4.5 && Inputmatrix(t,1)<5
       Outputmatrix(t,20)=Inputmatrix(t,2);
    end
end


for f=1:size(Outputmatrix,2)
    Outputmatrix2(f,1)=nanmedian(Outputmatrix(:,f));
end

%ensure minimal data points per bin (3)
NumOfDataPoints=isnan(Outputmatrix);
for q=1:size(Outputmatrix,2)
    temp=find((NumOfDataPoints(:,q))==0);
    if size(temp,1)>2
    Outputmatrix2(q,2)=1;
    else
    Outputmatrix2(q,2)=0;    
    end
end


end % function end
