MATLAB: How to replace elements in an array with the indices of the array’s sorted, unique values

indexingreplace elements

This is what I have (below). I am wondering if there is a better way of accomplishing the aforementioned task. Also, is there a name for this process? I don't have a formal computer science education so I'm curious.
A = randi(9,5,2); % Initialize matrix.
uA = unique(A); % Unique values of A.
B = A; % Output.
for idx = 1:length(uA) % Index to unique values, replace with
B(B == uA(idx)) = idx; % their sorted indices.
end

Best Answer

All you need is the third output of unique followed by reshape:
>> [~,~,Y] = unique(A);
>> reshape(Y,size(A))
For example:
>> B % from running your code
B =
4 5
2 3
1 4
4 7
5 6
>> [~,~,Y] = unique(A);
>> reshape(Y,size(A))
ans =
4 5
2 3
1 4
4 7
5 6