function [Outputmatrix2, Outputmatrix] = averageEye_V3(Inputmatrix)
% Inputmatrix is a n x 2  matrix with n=1: eyeposition 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 2°
Outputmatrix=nan(size(Inputmatrix,1),40);

for t=1:size(Inputmatrix,1)
    if Inputmatrix(t,1)>=-40 && Inputmatrix(t,1)<-38
       Outputmatrix(t,1)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-38 && Inputmatrix(t,1)<-36
       Outputmatrix(t,2)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-36 && Inputmatrix(t,1)<-34
       Outputmatrix(t,3)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-34 && Inputmatrix(t,1)<-32
       Outputmatrix(t,4)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-32 && Inputmatrix(t,1)<-30
       Outputmatrix(t,5)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-30 && Inputmatrix(t,1)<-28
       Outputmatrix(t,6)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-28 && Inputmatrix(t,1)<-26
       Outputmatrix(t,7)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-26 && Inputmatrix(t,1)<-24
       Outputmatrix(t,8)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-24 && Inputmatrix(t,1)<-22
       Outputmatrix(t,9)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-22 && Inputmatrix(t,1)<-20
       Outputmatrix(t,10)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-20 && Inputmatrix(t,1)<-18
       Outputmatrix(t,11)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-18 && Inputmatrix(t,1)<-16
       Outputmatrix(t,12)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-16 && Inputmatrix(t,1)<-14
       Outputmatrix(t,13)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-14 && Inputmatrix(t,1)<-12
       Outputmatrix(t,14)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-12 && Inputmatrix(t,1)<-10
       Outputmatrix(t,15)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-10 && Inputmatrix(t,1)<-8
       Outputmatrix(t,16)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-8 && Inputmatrix(t,1)<-6
       Outputmatrix(t,17)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-6 && Inputmatrix(t,1)<-4
       Outputmatrix(t,18)=Inputmatrix(t,2);
    end    
    if Inputmatrix(t,1)>=-4 && Inputmatrix(t,1)<-2
       Outputmatrix(t,19)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=-2 && Inputmatrix(t,1)<0
       Outputmatrix(t,20)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=0 && Inputmatrix(t,1)<2
       Outputmatrix(t,21)=Inputmatrix(t,2);
    end    
    if Inputmatrix(t,1)>=2 && Inputmatrix(t,1)<4
       Outputmatrix(t,22)=Inputmatrix(t,2);
    end   
    if Inputmatrix(t,1)>=4 && Inputmatrix(t,1)<6
       Outputmatrix(t,23)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=6 && Inputmatrix(t,1)<8
       Outputmatrix(t,24)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=8 && Inputmatrix(t,1)<10
       Outputmatrix(t,25)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=10 && Inputmatrix(t,1)<12
       Outputmatrix(t,26)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=12 && Inputmatrix(t,1)<14
       Outputmatrix(t,27)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=14 && Inputmatrix(t,1)<16
       Outputmatrix(t,28)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=16 && Inputmatrix(t,1)<18
       Outputmatrix(t,29)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=18 && Inputmatrix(t,1)<20
       Outputmatrix(t,30)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=20 && Inputmatrix(t,1)<22
       Outputmatrix(t,31)=Inputmatrix(t,2);
    end   
    if Inputmatrix(t,1)>=22 && Inputmatrix(t,1)<24
       Outputmatrix(t,32)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=24 && Inputmatrix(t,1)<26
       Outputmatrix(t,33)=Inputmatrix(t,2);
    end       
    if Inputmatrix(t,1)>=26 && Inputmatrix(t,1)<28
       Outputmatrix(t,34)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=28 && Inputmatrix(t,1)<30
       Outputmatrix(t,35)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=30 && Inputmatrix(t,1)<32
       Outputmatrix(t,36)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=32 && Inputmatrix(t,1)<34
       Outputmatrix(t,37)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=34 && Inputmatrix(t,1)<36
       Outputmatrix(t,38)=Inputmatrix(t,2);
    end
    if Inputmatrix(t,1)>=36 && Inputmatrix(t,1)<38
       Outputmatrix(t,39)=Inputmatrix(t,2);
    end    
    if Inputmatrix(t,1)>=38 && Inputmatrix(t,1)<40
       Outputmatrix(t,40)=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
