function [u, v] = best_rect_fit(small, big)

s = size(small);
b = size(big);
i = bsxfun(@plus, (1:s(1)).', b(1)*(0:s(2)-1));
shift = bsxfun(@plus, (0:b(1)-s(1)).', b(1)*(0:b(2)-s(2)));

idx = bsxfun(@plus, i(:), shift(:).');

diff = bsxfun(@minus, big(idx), small(:));
[~, minidx] = min(dot(diff, diff, 1));
[u, v] = ind2sub(size(big), shift(minidx)+1);

end
