This is easy with sub2ind:
>> A0 = [-1,1,-1,1;-1,-1,1,1;1,1,-1,-1;1,-1,1,-1] A0 = -1 1 -1 1 -1 -1 1 1 1 1 -1 -1 1 -1 1 -1 >> B0 = [0.1,-0.3,2,0.5;-0.9,0,1,1.5;1.5,1.2,0,-0.3;0.9,0.1,1.2,0.3] B0 = 0.10000 -0.30000 2.00000 0.50000 -0.90000 0.00000 1.00000 1.50000 1.50000 1.20000 0.00000 -0.30000 0.90000 0.10000 1.20000 0.30000 >> [A1,idc] = sort(A0,2); % sort, get column indices >> A1 = -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 >> Sz = size(A0); >> idr = repmat(1:Sz(1),Sz(2),1).'; % get row indices >> B1 = B0(sub2ind(Sz,idr,idc)) % convert column&row indices to linear. B1 = 0.10000 2.00000 -0.30000 0.50000 -0.90000 0.00000 1.00000 1.50000 0.00000 -0.30000 1.50000 1.20000 0.10000 0.30000 0.90000 1.20000