- Once you evaluate the below details you get the answer
- What is the values of x1,x2,x3 ?
- How do you relate the given vector with ranking information?
- What is the logic behind the final matrix?
Vector ranking and transformation matrix
조회 수: 3 (최근 30일)
이전 댓글 표시
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.
댓글 수: 2
Stalin Samuel
2016년 9월 21일
채택된 답변
Matt J
2016년 9월 21일
편집: Matt J
2016년 9월 21일
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))
댓글 수: 6
Matt J
2016년 9월 21일
Hmmm. The discrepancy disappeared after I re-pasted the for-loop code. In any case, here is an improved version for which I see a few factors speed-up over the loops.
x=randperm(1000).';
tic
n=length(x);
[I,J]=ndgrid(1:n);
idx=J>I;
m=nnz(idx);
B=sparse(1:m,J(idx),1,m,n) - sparse(1:m, I(idx),1,m,n);
result=bsxfun(@times, sign(B*x), B);
toc
%Elapsed time is 0.685717 seconds.
tic
T=length(x);
X=[x [1:T]'];
k=sortrows(X);
V=k(:,2);
s=1;Q=zeros(T*(T-1)/2,T);
for i =1:T
for j =1:T-i
Q(s,V(i))=-1;Q(s,V(i+j))=1;s=s+1;
end
end
toc
%Elapsed time is 2.316114 seconds.
추가 답변 (1개)
Steven Lord
2016년 9월 21일
If you're asking how to convert the inequalities (like -x1 + x2 < 0) into matrix form, I don't know if there's a function to do exactly that but the equationsToMatrix function comes close. You may be able to slightly modify your inequalities so they are equations then use equationsToMatrix to generate the matrices to use as your A, b, Aeq, and beq inputs to the Optimization Toolbox solvers (which is how I'm assuming you're planning to use those matrices.)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!