function [index,value] = closest(find_value_x, in_y)

% Find closest value of x in array y

x = find_value_x;
y = in_y;

[m,n] = size(x);
index = zeros(m,n);

for k = 1:m
    if ~iscolumn(y)
        y=y';
    end
    
    [~,index(k,:)] = min(abs( bsxfun(@minus, y, x(k,:)) ));
end

if nargout>1
    value = y(index);
end

end