Converts [L,d]
from ldfactor
into the original matrix that the
factors represent, L * diag(d) * L.'
, with minimal operations.
This also works with multiple sets of L
and d
, stored along the 3rd
and 2nd dimension of each, respectively.
M = ld2mat(L, d)
Finally, this works with the compact form of LDL factorization, where d
is not created explicitly and is stored on the diagonal of L
.
L | Lower unitriangular part of LDL factorization |
---|---|
d | Diagonals of UDU factorization |
M | Reconstructed matrix (or matrices) |
---|
Let's generate a positive-definite matrix, factorize, and show that the factors reconstruct properly:
M = randcov(3)
[L, d] = ldfactor(M);
L * diag(d) * L.'
ld2mat(L, d)
M =
0.5052 -0.2311 -0.2059
-0.2311 0.5124 -0.0714
-0.2059 -0.0714 0.7502
ans =
0.5052 -0.2311 -0.2059
-0.2311 0.5124 -0.0714
-0.2059 -0.0714 0.7502
ans =
0.5052 -0.2311 -0.2059
-0.2311 0.5124 -0.0714
-0.2059 -0.0714 0.7502
We can also use ld2mat
with the compact LDL decomposition to save space
and time.
Z = ldfactor(M);
ld2mat(Z)
ans =
0.5052 -0.2311 -0.2059
-0.2311 0.5124 -0.0714
-0.2059 -0.0714 0.7502
Let's generate a positive-definte matrix for each of 10 samples of each of 5 runs.
nx = 3;
ns = 10;
nr = 5;
M = zeros(nx, nx, ns, nr);
L = zeros(nx, nx, ns, nr);
d = zeros(nx, ns, nr);
for s = 1:ns
for r = 1:nr
M(:, :, s, r) = randcov(nx);
[L(:, :, s, r), d(:, s, r)] = udfactor(M(:, :, s, r));
end
end
Let's reconstruct all of the matrices in M all at once and show the difference.
M2 = ld2mat(L, d);
max(abs(M(:) - M2(:)))
ans =
0.5236
ldfactor, udfactor, ud2mat, sqrtpsdm
*kf
v1.0.3