function newYY = inelastic_collisions(YY)

global M G N coll_distance
R=YY(:,1:3);
V=YY(:,4:6);
%Vy=YY(:,4);

remove_list=[];
for i=1:N
    for j=1:N
        if i~=j 
            if norm(R(i,:)-R(j,:))<coll_distance 
                if 0==ismember(i,remove_list) && 0==ismember(j,remove_list)
                    new_V=(M(i)*V(i,:)+M(j)*V(j,:))./(M(i)+M(j));
                    M(i)=M(i)+M(j);
                    N=N-1;
                    YY(i,4:6)=new_V;
                    remove_list=[remove_list j];
                end
               
               
            end
        end
    end
end

YY(remove_list,:)=[];
M(remove_list)=[];
newYY=YY;

                
                
                
                