LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
SimilarityMatrixForVectors.m
1% B = SimilarityMatrixForVectors(vecA, vecB)
2%
3% Returns the similarity transformation matrix that converts
4% a given column vector to an other column vector. It works
5% even with zero entries.
6%
7% Parameters
8% ----------
9% vecA : column vector, shape(M,1)
10% The original column vector
11% vecB : column vector, shape(M,1)
12% The target column vector
13%
14% Returns
15% -------
16% B : matrix, shape(M,M)
17% The matrix by which `B\cdot vecA = vecB` holds
18
19function B = SimilarityMatrixForVectors (vecA, vecB)
20
21 % construct permutation matrix to move at least one non-zero element to the first position
22 % to acchieve it, the code below sorts it in a reverse order
23 m = length(vecA);
24
25 [~,ix] = sort(-vecA);
26 P = zeros(m);
27 for i=1:m
28 P(i,ix(i)) = 1.0;
29 end
30 cp = P*vecA;
31
32 % construct transformation matrix B for which B*rp=1 holds
33 B = zeros(m);
34 for i=1:m
35 B(i,1:i) = vecB(i) / sum(cp(1:i,1));
36 end
37 % construct matrix B for which B*r=1 holds
38 B = B*P;
39end