Hi sorry, i want to create a code to know if a generic matrix A is positive definite. On my version of matlab (2016) when i write the second & the code gives me an error saying that "Matrix dimensions must agree". Can you help me?
A=[13/4 1/2 0; 1/2 17/4 1/2; 0 1/2 21/4]
A = 3×3
3.2500 0.5000 0 0.5000 4.2500 0.5000 0 0.5000 5.2500
[n,m]=size(A);
D_A=diag(A);
M_A=A-diag(D_A);
if n==m & A==A' & (abs(D_A)>sum(abs(M_A),2) | abs((D_A)')>sum(abs(M_A),1)) & diag(A)>0
disp 'A è DEFINITA POSITIVA'
end
A è DEFINITA POSITIVA

댓글 수: 12

Voss
Voss 2021년 12월 28일
Use && to short-circuit. Requires scalar inputs.
Marco Lepidi
Marco Lepidi 2021년 12월 28일
If i use && i have this error "Operands to the || and && operators must be convertible to logical scalar values."
the cyclist
the cyclist 2021년 12월 28일
Is the code you posted exactly the code that gives the error? It doesn't give an error in 2021b, and I don't see specifically what would give an error in a prior version.
Voss
Voss 2021년 12월 28일
Like I said, "Requires scalar inputs."
Marco Lepidi
Marco Lepidi 2021년 12월 28일
Yes the code is exactly the same. Here on the 2021 it doesn't give errors. On my matlab it does
Marco Lepidi
Marco Lepidi 2021년 12월 28일
so @Benjamin what i have to do? i need this 4 coditions to say that the matrix is positive definite
Voss
Voss 2021년 12월 28일
It seems like you are using a non-square matrix A to get the error you report, but the matrix here is square so we get no error.
DGM
DGM 2021년 12월 28일
This
A==A.'
returns a vector, but this:
isequal(A,A.')
returns a scalar.
Voss
Voss 2021년 12월 28일
I don't know if this is precisely the condition you want, but you might try something like this:
if n==m && isequal(A,A') && (all(abs(D_A)>sum(abs(M_A),2)) || all(abs((D_A)')>sum(abs(M_A),1))) && all(diag(A)>0)
Marco Lepidi
Marco Lepidi 2021년 12월 28일
@Benjamin with your code the problem is solved thank you. But can you explain me what isequal and all do?
Voss
Voss 2021년 12월 28일
isequal is for comparing arrays (especially non-scalar arrays). all is for checking a condition on elements of an array.
Marco Lepidi
Marco Lepidi 2021년 12월 28일
@Benjamin thank you very much

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

질문:

2021년 12월 28일

댓글:

2021년 12월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by