if statement with and or
古いコメントを表示
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]
[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
12 件のコメント
Voss
2021 年 12 月 28 日
Use && to short-circuit. Requires scalar inputs.
Marco Lepidi
2021 年 12 月 28 日
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
2021 年 12 月 28 日
Like I said, "Requires scalar inputs."
Marco Lepidi
2021 年 12 月 28 日
Marco Lepidi
2021 年 12 月 28 日
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
2021 年 12 月 28 日
This
A==A.'
returns a vector, but this:
isequal(A,A.')
returns a scalar.
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
2021 年 12 月 28 日
Marco Lepidi
2021 年 12 月 28 日
回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!