Permutation Matrix on a Vector
7 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Matt Fig
2011-3-19
If I understand your question correctly, there is no unique solution. You can understand this by looking at a 2-by-2 system.
P = [a b;c d]
V = [e;f]
Vs = [g;h]
Now from P*V = Vs, we know that:
a*e + b*f = g
c*e + d*f = h
Thus if V and Vs are known, then e, f, g, and h are known. But we still have two equations and four unknowns ( a , b, c, and d ). So unless there are two more ways of limiting the choices for the four unknowns, we are stuck.
For a larger system, the problem only gets worse.
2 个评论
Jan
2011-3-19
But in addition we have the term "permutation matrix". This means that a,b,c,d are 0 or 1 with only one 1 per row and column.
更多回答(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 个评论
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');
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!