% calculate the distance
% input must be row vector

function [Dist] = Dis_fun(X, Y, flag, Weight, dis) % dis: type of distance functions, 1: Euclidean, 2: Manhatton

[junk, cx] = size(X);
Dist = 0;
Dis(1, cx) = 0;

for i = 1 : cx
    
    if flag(1, i) == 0 % if the attribute is categorical
        
        if X(1, i) == Y(1, i) % if the attributes i of X and Y are equal
            
            Dis(1, i) = 0;
            
        else 
            
            Dis(1, i) = 1;
            
        end
        
    else % if the attribute is numerical
        if dis == 1 
            
           Dis(1, i) = (X(1,i) - Y(1,i)).^2;
           
        elseif dis == 2
            
           Dis(1, i) = abs(X(1,i) - Y(1,i));
           
        end
       
    end
    
end

cap = size(Weight); %used to catch case in which no weight attached
if cap ~= 0
Dist = sum(Weight.* Dis);
end
