function [distances] = updownstream_distances(segment_locations,...
    elem_count)

segs = elem_count-1;
lengths = segment_locations(:,5);
distances = zeros(segs,segs);

for j = 1:segs
    
    if j == 1
        
        for k = 1:segs            
            if k == j
                distances(k,j) = lengths(k,1);
            else
                distances(k,j) = distances(k-1,j) + lengths(k,1);
            end          
        end
        
    elseif j > 1 && j < segs
        
        distances(j,j) = lengths(j,1);
        
        for k = j-1:-1:1            
            if k == j-1
                distances(k,j) = lengths(k,1) + distances(j,j) * 0.5;
            else
                distances(k,j) = distances(k+1,j) + lengths(k,1);
            end           
        end
        
        for k = j+1:segs            
            if k == j+1
                distances(k,j) = lengths(k,1) + distances(j,j) * 0.5;
            else
                distances(k,j) = distances(k-1,j) + lengths(k,1);
            end           
        end
        
    elseif j == segs
        
        distances(j,j) = lengths(j,1);
        
        for k = j-1:-1:1           
            if k == j-1
                distances(k,j) = lengths(k,1) + distances(j,j) * 0.5;
            else
                distances(k,j) = distances(k+1,j) + lengths(k,1);
            end           
        end 
        
    end 
    
end