function [Y] = reconstruct(X)
%% Y
%% X is the input matrix, an -1 element means the missing element

    [n m]=size(X); % V contains your data in its column vectors
    Y=zeros(n,m);
    for i = 1:n
        for j = 1:m
            if (i ~= j)
                if (X(i,j)<0)
%                     closest = 1; closest_value = X(1, j);
%                     for k=1:n
%                         if ((X(k, j) < closest_value | (closest_value < 0 & X(k,j) > 0)) & X(i,k)>0)
%                             closest = k;
%                             closest_value = X(k, j);
%                         end
%                     end
%                     Y(i,j) = X(i, k);
                    L=0; U=10000;
                    % L=max(abs(X(i,k)-X(k,j))); U=min(X(i,k)+X(k,j));
                    for k=1:n
                        if (X(i,k)>=0 & X(k,j)>=0)
                            x1 = abs(X(i,k)-X(k,j));
                            if (x1 > L)
                                L=x1;
                            end
                            x2 = X(i,k)+X(k,j);
                            if (x2 < U)
                                U = x2;
                            end                            
                        end
                    end
                    Y(i,j)=(L+U)/2;
                else
                    Y(i,j) = X(i,j);
                end            
            else
                Y(i,j)=X(i,j);
                if (Y(i,j)<0)
                    Y(i,j) = 0;
                end
            end
        end
    end
    return