ld2mat

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.

Inputs

L Lower unitriangular part of LDL factorization Diagonals of UDU factorization

Outputs

M Reconstructed matrix (or matrices)

Example: Basic Case

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

Example: Compact Form

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

Example: Dimensions 3 and 4

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