MATLAB: Replace matrix elements by the difference with the highest alternative in the same row

MATLABmaxreplace

How to obtain the third matrix given the original matrix?
●The original matrix:
1 6 13
3 9 2
7 11 20
●The difference with the highest alternative in the same row will be:
1-13 6-13 13-6
3-9 9-3 2-9
7-20 11-20 20-11
●which is:
-12 -7 7
-6 6 -7
-13 -9 9

Best Answer

clc; clear all;
A = [1 6 13
3 9 2
7 11 20] ;
iwant = zeros(size(A)) ;
for i = 1:size(A,1)
for j = 1:size(A,2)
iwant(i,j) = A(i,j)-max(setdiff(A(i,:),A(i,j))) ;
end
end
iwant