matlab双重比较 [英] matlab double comparison

查看:194
本文介绍了matlab双重比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个双精度数组与一个标量二进制进行比较,以获得平等,但是在某些情况下,平等就不会被识别。我怀疑这与双重代表的方式有关(例如,1.0 vs 1.00),但我无法理解。

I am trying to compare an array of doubles to a scalar double for equality, but equality is never recognized under certain circumstances. I suspect that this has to do with the way the double is represented (e.g., 1.0 vs 1.00), but I can't figure it out.

例如,我已经生成了一个由数千个双重值组成的数组,其中最后几个在时间上由

For instance, I have generated an array composed of thousands of double values, the last few of which at some instant in time are given by

10.6000
-11.0000
10.2000
22.6000
3.4000

如果我通过命令 array == 10.2 (或 array = 10.2000 )测试10.2(或10.2000)我返回0的数组。如果我将显示的值手动放置到数组中(例如, array = [10.6000 -11.0000 10.2000 22.6000 3.4000] ),则命令成功(即 array == 10.2 返回 0 0 1 0 0 )。有人可以解释为什么如果手动输入值等于成功,但如果数组是在程序的上下文中生成的,则会失败?我可以通过使用近似而不是精确的比较来纠正比较失败(例如,(array< 10.20001)&(array> 10.19999)),但是这似乎不满意。

If I test for equality to 10.2 (or 10.2000) by the command array==10.2 (or array=10.2000), I return an array of 0s. If I place the values shown into an array manually (e.g., array=[10.6000 -11.0000 10.2000 22.6000 3.4000]), then the command is successful (i.e., array==10.2 returns 0 0 1 0 0). Could someone please explain why the equality succeeds if I input the values manually, but fails if the array is generated in the context of a program? I am able to rectify the comparison failure by using an approximate rather than an exact comparison (e.g., (array<10.20001) & (array>10.19999)), but this seems unsatisfying.

编辑:数组中的值通过迭代加法或减法来生成常量double(例如, 0.2 )。因此,该数组的模数由 0.2 应该在任何地方等于 0 。实际上,每个元素的模数等于 0 0.2 ,如下所示数组中的数字:

The values in the array are generated by iterative addition or subtraction of a constant double (e.g., 0.2). The modulus of this array by 0.2 should therefore be everywhere equal to 0. In fact, the modulus of each element is equal to either 0 or 0.2, as shown below for the above sequence of numbers in the array:

mod(array,0.2)
...
0.2000
     0
0.2000
0.2000
     0

再次,如果值被手动放置在数组中,并且采用模数,获得所有 0 s的预期值。

Again, if the values are placed in an array manually and the modulus is taken, the expected value of all 0s is obtained.

推荐答案

原因是MATLAB截断数组中的数字,以便在显示时仅保留小数点后的4位数字。也就是说,您的数组的实际值可能是 [10.600000000001,-10.99999999999,...] 。你是对的,这是由于计算机中浮点数的内部表示,这可能会导致计算中的微小错误。

The reason is MATLAB truncated the numbers in the array to preserve only 4 digits after the decimal point when displaying,. That is, the real value of your array may be [10.600000000001, -10.99999999999, ...]. You are right, this is due to the internal representation of floating-point numbers in computer, which may cause tiny errors in the computations.

我个人认为有两个解决方案,一个是像你这样做的近似匹配,而另一个是先排列阵列(比如,使用此工具),然后做完全匹配。

Personally I think there are two solutions, one is approximate matching like you did, while the other is to round the array up first (say, with this tool from FileExchange), and then do exact matching.

这篇关于matlab双重比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆