MATLAB: Vector ranking and transformation matrix

transformation matrixvector ranking

Hello. Suppose we have a vector [1 4 3], here -x1+x2>0, -x1+x3>0 and also x2-x3>0. How can we transform this ranking information into a matrix like [-1 1 0; -1 0 1; 0 1 -1]? Is there a function to realize it? Thank you in advance for your time and help.

Best Answer

n=length(x);
A=nchoosek(1:n,2);
m=size(A,1);
B=sparse(1:m, A(:,1),1,m,n) - sparse(1:m, A(:,2),1,m,n);
result=full(bsxfun(@times, sign(B*x), B))