Neglecting difference between two values if it is very small so that value1= value2

I am working on a dataset. After some eigendecompostion process. I realise the difference between my values are as small as 1.8097e-14 which is my further implementation process.
Example
data1(1:10) = 0 0.1502 0.2970 0.4371 0.5673 0.6845 0.7863 0.8702 0.9343 0.9773
data2(1:10) = -0.0000 0.1502 0.2970 0.4371 0.5673 0.6845 0.7863 0.8702 0.9343 0.9773
C= data1(1:10)-data2(1:10)
C =
1.0e-12 *
0.0862 0.1227 0.1545 0.0772 0.1019 0.1178 -0.0174 0.0048 0.0170 0.0181
How do I code that the these difference be neglected so that values in data1 & data2 will be equal since the difference is very small

Antworten (1)

Fangjun Jiang
Fangjun Jiang am 28 Jul. 2021
Bearbeitet: Fangjun Jiang am 28 Jul. 2021
To compare element by element, abs(C) < Tolerance
To compare the two vectors as a whole, all(abs(C) < Tolerance)
or use
ismembertol(C,0,Tolerance)

7 Kommentare

Cutie
Cutie am 29 Jul. 2021
Bearbeitet: Cutie am 29 Jul. 2021
Thank you for your answer but Can you please explain the 'tolerance' how I can define it. I can't find it as inbuilt function in MATLAB.
It is a small number like the one @Stephen Cobeldick gave. It is the difference that can be neglected based on the meaning of your data.
Hmmm....I am not clear yet.
Let me simply put it that if I have two data.
Data1=2.0000000001
Data2 = 2.0
Data1-Data2 = 1e-10 (which is very small).
How do I write my code to neglect this difference in such as way that Data1-Data2 will output 0 (i.e Data1-Data2 = 0) instead of 1e-10
You goal is to compare if Data1 and Data2 are equal,
instead of using "if Data1==Data2 (or Data1-Data2==0)" which are problematic,
you use "if abs(Data1-Data2)<1e-6"
@Fangjun Jiang will it look like
if abs(Data1-Data2)<1e-6
Data1=Data2
end
I guess you could to that. The solution is clear, do whatever to get the result you need.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021a

Gefragt:

am 28 Jul. 2021

Kommentiert:

am 29 Jul. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by