Similar to abhineetprasad's solution, but you don't need a key–value structure.
You can use almost the same method for matrices as for vectors. You just need to determine the sort indices for A with respect to the vector-shaped version A(:)
and initialize B to the same dimensions as A. Then you can use linear indexing into the matrix B to fill it with the ranks:
% prepare matrix B with the same dimensions as A
B = zeros(size(A));
% determine sort indices of the matrix entries treated as a vector
[~, ind] = sort(A(:));
% use linear indexing by sort indices to fill vector B with ranks
B(ind) = 1 : numel(B);