MATLAB: How to replace the values of a matrix

matrix manipulation max

Hello everyone, I have a matrix A : size m*n (4 * 10) and I want to operate this matrix line by line .
A=[0.82 0.36 0.25 0.95 0.66 0.88 0.96 0.11 0.55 0.47
0.25 0.55 0.33 0.85 0.99 0.37 0.21 0.12 0.44 0.66
0.85 0.11 0.65 0.89 0.59 0.24 0.57 0.33 0.45 0.90
0.38 0.54 0.85 0.44 0.21 0.69 0.11 0.22 0.02 0.88]
for each row of A, I want to find the maximum and replace by n, then look up again and replace by n-1 and so on. for example, the maximum value of the first row of the matrix A is 0.96 so I replaced it with 10 after another maximum value is 0.95, it will be replaced by 9 and the same for other values. Finally, I want to get a new matrix A.
A=[7 3 2 9 6 8 10 1 5 4
3 7 4 9 10 5 2 1 6 8
8 1 7 9 6 2 5 3 4 10
5 7 9 6 3 8 2 4 1 10]
please helpe me, how can I implement this problem in matlab? and thanks in advance.

Best Answer

Use the second output of sort to help you:
newA = zeros(size(A));
for rowidx = 1:size(A, 1)
row = A(rowidx, :);
[~, order] = sort(row); %get the order of the values in the row
newA(rowidx, order) = 1:numel(row); %and use that order to replace the elements by 1:10
end