Remove negative 0 previous solution does not work in this context

1 回表示 (過去 30 日間)
Mike
Mike 2022 年 6 月 29 日
コメント済み: Corey McDowell 2022 年 6 月 29 日
Attempting to use the solution which removes negative 0 as shown by Rick here:
In does not seem to work for me in this context why?
exp_phase_shift is a 1x8
I can see element 7 a small value of something to the -16
I am able to use this solution:
smallValues = abs(exp_phase_shift_re) < 0.000000001;
exp_phase_shift_re(smallValues) = 0;
Want to know why:
A(A==0)=0;
Does not work
Thanks
Mike

採用された回答

Corey McDowell
Corey McDowell 2022 年 6 月 29 日
the equality operator is not a very good fit for floating point numbers. you should instead specify a tolerance
% random values in the range (a,b)
a = 0;
b = 2E-5;
A = a + (b-a)*rand(1,10)
A = 1×10
1.0e-04 * 0.1177 0.0540 0.0583 0.0653 0.1559 0.1669 0.1893 0.1672 0.1605 0.1082
% this is the solution you want
tol = 1E-5;
A(abs(A-0) < tol) = 0
A = 1×10
1.0e-04 * 0.1177 0 0 0 0.1559 0.1669 0.1893 0.1672 0.1605 0.1082
  1 件のコメント
Corey McDowell
Corey McDowell 2022 年 6 月 29 日
to be clearer, the equality operator does not work because it does not have a tolerance, 1E-15 is a different value from 0, so they are not equal. the solution you reference is specifically talking about signed 0 which is arithmatically identical to a 0, but symbolically is a distinction between the two onesided limits as they are approach 0

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by