How to use 'if else' in this case?

1 visualizzazione (ultimi 30 giorni)
Armando MAROZZI
Armando MAROZZI il 30 Dic 2020
Commentato: Armando MAROZZI il 30 Dic 2020
I have the following variance-covariance matrix which is not positive definite:
x = [183.5310 -0.4098 8.0035 -0.0333 -0.1818 -0.9685 0.2681 0.1241 -8.3334;
-0.4098 2.5004 0.7254 0.0150 0.2729 0.0262 0.0390 -0.0128 -0.5049;
8.0035 0.7254 163.7246 -0.0541 -1.1285 0.8830 -0.0161 -0.2604 -0.5571;
-0.0333 0.0150 -0.0541 0.0060 -1.4514 -2.4601 0.0049 -0.0071 -0.0297;
-0.1818 0.2729 -1.1285 -1.4514 -318.8469 -607.2140 -0.0163 -1.7903 -1.4473;
-0.9685 0.0262 0.8830 -2.4601 -607.2140 -924.7691 -0.1620 -2.8692 -1.1332;
0.2681 0.0390 -0.0161 0.0049 -0.0163 -0.1620 0.0772 -0.0070 -0.0476;
0.1241 -0.0128 -0.2604 -0.0071 -1.7903 -2.8692 -0.0070 0.0076 -0.0175;
-8.3334 -0.5049 -0.5571 -0.0297 -1.4473 -1.1332 -0.0476 -0.0175 21.2720]
I can correct it and make it positive definite in this way:
[V,D] = eig(x); % Calculate the eigendecomposition of your matrix (A = V*D*V')
% where "D" is a diagonal matrix holding the eigenvalues of your matrix "A"
d= diag(D); % Get the eigenvalues in a vector "d"
d(d <= 1e-7) = 1e-7; % Set any eigenvalues that are lower than threshold "TH" ("TH" here being
% equal to 1e-7) to a fixed non-zero "small" value (here assumed equal to 1e-7)
D_c = diag(d); % Built the "corrected" diagonal matrix "D_c"
PphiTilde = V*D_c*V'; % Recalculate your matrix "A" in its PD variant "A_PD"
Yet, I would like to write a code that says: if the matrix is not positive definite then do what I did above. I tried unsuccessfully:
if chol(x) == 'Matrix must be positive definite.'
[V,D] = eig(x); % Calculate the eigendecomposition of your matrix (A = V*D*V')
% where "D" is a diagonal matrix holding the eigenvalues of your matrix "A"
d= diag(D); % Get the eigenvalues in a vector "d"
d(d <= 1e-7) = 1e-7; % Set any eigenvalues that are lower than threshold "TH" ("TH" here being
% equal to 1e-7) to a fixed non-zero "small" value (here assumed equal to 1e-7)
D_c = diag(d); % Built the "corrected" diagonal matrix "D_c"
PphiTilde = V*D_c*V'; % Recalculate your matrix "A" in its PD variant "A_PD"
else x
end
Can anyone help me?
Thanks!

Risposta accettata

Steven Lord
Steven Lord il 30 Dic 2020
See the description of the second output from the chol function on its documentation page.
  2 Commenti
madhan ravi
madhan ravi il 30 Dic 2020
help chol
CHOL Cholesky factorization. R = CHOL(A) calculates the Cholesky factor of full or sparse A using the diagonal and upper triangle of A, such that A = R'*R. A must be positive definite, and the lower triangle is assumed to be the (complex conjugate) transpose of the upper triangle. R = CHOL(A,TRIANGLE) specifies which triangle of A to use. For example, if TRIANGLE is 'lower', then CHOL uses only the diagonal and lower triangle of A to produce a lower triangular R such that A = R*R'. The default value of TRIANGLE is 'upper'. Using the 'lower' option is equivalent to calling CHOL with the 'upper' option and the transpose of A, and then transposing the output matrix R. In the following syntaxes, the output R is described only for the case when TRIANGLE is 'upper'. [R,FLAG] = CHOL(___) also returns the output FLAG indicating whether A is positive definite. Use this syntax to suppress errors produced when A is not positive definite. * If A is positive definite, then FLAG is 0 and R is the same as above. * If A is not positive definite, then FLAG is a positive integer. - If A is full, then R is an upper triangular matrix of size q = FLAG-1 such that R'*R = A(1:q,1:q). - If A is sparse, then R is an upper triangular matrix of size q-by-n such that the L-shaped region of the first q rows and first q columns of R'*R agree with those of A. [R,FLAG,P] = CHOL(S), for sparse S, additionally returns a permutation matrix P, which is a preordering of S obtained by AMD. The Cholesky factor of P'*S*P tends to be sparser than that of S. If FLAG = 0, then R is an upper triangular matrix such that R'*R = P'*S*P. [R,FLAG,p] = CHOL(___,OUTPUTFORM) specifies whether to return the permutation information as a vector p or matrix P. With this syntax you must specify a sparse input matrix S, and you optionally can specify TRIANGLE. For example, if OUTPUTFORM is 'vector' and FLAG = 0, then S(p,p) = R'*R. The default value of OUTPUTFORM is 'matrix' such that P'*S*P = R'*R. See also CHOLUPDATE, ICHOL, LDL, LU. Documentation for chol doc chol Other functions named chol codistributed/chol gpuArray/chol sym/chol
Armando MAROZZI
Armando MAROZZI il 30 Dic 2020
thanks a lot! sorted!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by