Why is the string below the first conditional not printed?

1 回表示 (過去 30 日間)
Andromeda
Andromeda 2021 年 11 月 13 日
コメント済み: dpb 2021 年 12 月 2 日
I need help understanding why the string below the first conditional is not printed when AA, BB, CC are clearly equal. See attachment below
  2 件のコメント
dpb
dpb 2021 年 11 月 13 日
(AA==BB) == 1 && (CC) == 1
is not good syntax.
if AA==1 & BB==1 & CC==1
will work; more succinct forms can be written if you would use arrays for variables instead of indivdually naming the variables (generally a bad practice to use MATLAB efficiently to have mulitple similarly-named/functionally equivalent variables as above).
Andromeda
Andromeda 2021 年 11 月 14 日
I tried if (AA == BB) == (CC ==1) first and that didn't produce the expected output and thus resorted to what you see above. Why doesn't if (AA == BB) == (CC ==1) work as well?
"if you would use arrays for variables instead of indivdually naming the variables ", care to give me an example?

サインインしてコメントする。

回答 (1 件)

Cris LaPierre
Cris LaPierre 2021 年 11 月 14 日
編集済み: Cris LaPierre 2021 年 11 月 14 日
Because the result of the dot products is not exactly 1.
A = [2 2 -1+4i]/5;
B = circshift(A,-1);
C = circshift(B,-1);
AA = dot(A,A)
AA = 1.0000
BB = dot(B,B)
BB = 1.0000
CC = dot(C,C)
CC = 1.0000
% now subtract 1. Result is not 0
AA-1
ans = 2.2204e-16
BB-1
ans = 2.2204e-16
CC-1
ans = 2.2204e-16
  2 件のコメント
Andromeda
Andromeda 2021 年 12 月 2 日
Well, that is odd. For a vector to form an orthonormal basis[algebra], the dot product of the vector with itself should be equal to 1
dpb
dpb 2021 年 12 月 2 日
Floating point arithmetic is only an approximation to algebra...see <Goldberg - What Every Computer Scientist Should Know> for detailed explanation. There are some other discussions and examples in the documentation as well.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by