[u_sort,index_u]  = sort(u(:)); 
[v_sort,index_v]  = sort(v(:));
uhistv(index_u) = v_sort;
uhistv = reshape(uhistv,size(u));  % use 'matrix' instead of 'reshape' with Scilab


figure;
subplot(1,3,1)
imshow(u);
title('u');
subplot(1,3,2)
imshow(v);
title('v');
subplot(1,3,3)
imshow(uhistv);
title('u matched with v');
drawnow