MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# why eq says they are not equal

Asked by Nuchto on 19 Jul 2012

I have two equal vectors a = b. When I check for equality:

```y = eq(a, b)
find(y==0)
```

I get most of values, meaning they are NOT equal.

What would be the reason for this?

N.

Jan Simon on 21 Jul 2012

Nuchto on 22 Jul 2012

Thanks, I did.

Jan Simon on 22 Jul 2012

## Products

No products are associated with this question.

Answer by Muruganandham Subramanian on 19 Jul 2012
Edited by Walter Roberson on 22 Jul 2012

Hi Nuchto,

http://www.mathworks.in/help/techdoc/ref/eq.html

## 1 Comment

Nuchto on 19 Jul 2012

I have checked the help, but it didn't answer to my question.

Answer by Jan Simon on 19 Jul 2012
Edited by Jan Simon on 19 Jul 2012

To compare two vectors use:

```r = isequal(a, b)
```

or to find the difference between vectors (or other arrays) of the same size:

```any(a - b)
```

Note, that the later replies FALSE even for ones(1,10) and ones(1,1).

If you only assume, that the vectors have equal values, check a - b and note, that the limited precision of the type double leads to effects like:

```03. - 0.2 ~= 0.1
```

Then check "almost equal" by:

```limit = eps(max(max(abs(a(:)), abs(b(:)))));
% or:   eps(max(abs(a(:)), abs(b(:))));
% or:   the same times 10
a_eq_b = isequal(size(a), size(b)) & all(abs(a - b) < limit);
```

The choise of the limit depends on the physical meaning of the variables. There is no unique definition to distinguish measurement noise from noise caused by numerical artifacts for a tiny values.