Permutation Matrix on a Vector
이전 댓글 표시
How can I compute the permutation matrix without using loop?
Let
V = Original n-vector
Vstar = Permuted n-vector
P = (n x n) Permutation Matrix
such that
P * V = Vstar.
Given V and Vstar, how can I determine P without using loop?
채택된 답변
추가 답변 (1개)
Jan
2011년 3월 19일
If the values of V are unique:
V = (1:5)';
Vstar = V(randperm(length(V)));
P = bsxfun(@eq, V', Vstar);
isequal(P * V, Vstar)
댓글 수: 4
Raymond's Group Lee
2011년 3월 19일
Matt Fig
2011년 3월 19일
Thanks Jan, I wasn't ready to assume this is what Raymond meant. You were correct.
Matt Fig
2011년 3월 19일
Jan, is this faster (though less comprehensible) on a newer version? On my 2007a, it is about 40% faster for N = 5000;
[VI,VI] = sort(V); % If V is sorted already, then skip this.
[VsI,VsI] = sort(Vstar);
P2 = zeros(N);
P2(VsI+(VI-1)*N) = 1;
Jan
2011년 3월 19일
Surprising! Two SORT compared to a simple comparison? BSXFUN seems to be worth to be improved. A further acceleration: P2 = zeros(N, N, 'uint8');
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!