matlab双重比较 [英] matlab double comparison

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

问题描述

我试图比较一个双精度数组和一个标量双精度的平等,但是平等在某些情况下是不会被识别的。我怀疑这与双重表示的方式有关(例如,1.0对1.00),但我不能想出来。



例如,我生成了由数千个双值组成的数组,其中最后几个时间由

给出

  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)),但这似乎不满意。



编辑:数组中的值是通过迭代加法或减法常数double生成的(例如 0.2 )。因此 0.2 的数组的模数应该无处不等于 0 。事实上,每个元素的模数等于 0 0.2 ,如下面针对上述序列数组中的数字:

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

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

解决方案

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



我个人认为有两个解决方案,一个是像你做的近似匹配,另一个是首先将数组舍入(例如,使用来自FileExchange的此工具),然后执行完全匹配。 / p>

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

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.

Edit: 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

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

解决方案

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天全站免登陆