Effacer les filtres
Effacer les filtres

Subtracting two values that are very small

2 vues (au cours des 30 derniers jours)
Muhammad Choudhury
Muhammad Choudhury le 11 Fév 2022
Commenté : Image Analyst le 12 Fév 2022
I'm subtracting two variables (R1 and R2) that are around a magnitude of e-3. I work out a difference between them (Diff = R2 - R1) but my Diff variable outputs as 0 for the whole matrix. I am trying to plot Diff on a plot. How can I make Diff not output as all 0's? Thank you.
R1 = [0.00212278260869565 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391]
R1 = 1×21
0.0021 0.0021 0.0021 0.0021 0.0021 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0023
R2 = [0.00212278260869565 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391]
R2 = 1×21
0.0021 0.0021 0.0021 0.0021 0.0021 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0022 0.0023
Diff = R2 - R1
Diff = 1×21
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  4 commentaires
Muhammad Choudhury
Muhammad Choudhury le 11 Fév 2022
Modifié(e) : Muhammad Choudhury le 11 Fév 2022
that is very true i didn't even realise, sorry about that, if my question still held, how would go about working it out?
William Rose
William Rose le 11 Fév 2022
@Muhammad Choudhury, if they were different then the vecotr
R2mR1=R2-R1;
wil have the correct difference.
I would not use
diff=R2-R1;
because there is a built-in function called diff() that computes the successive differences of a vector or array.

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 11 Fév 2022
R1 and R2 are equal, so their difference is in fact 0 everywhere.
R1 = [0.00212278260869565 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391];
R2 = [0.00212278260869565 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391];
Diff = R2 - R1
Diff = 1×21
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
isequal(R2,R1)
ans = logical
1
  3 commentaires
Voss
Voss le 11 Fév 2022
If they were different then the output from diff() would be non-zero somewhere. You can plot the difference the same way regardless:
R1 = [0.00212278260869565 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391];
% increased R2(1) by 1e-17:
R2 = [0.00212278260869566 0.00212931740869565 0.00213585398260870 0.00214239233043478 0.00214893245217391 0.00215547434782609 0.00216201801739130 0.00216856346086957 0.00217511067826087 0.00218165966956522 0.00218821043478261 0.00219476297391304 0.00220131728695652 0.00220787337391304 0.00221443123478261 0.00222099086956522 0.00222755227826087 0.00223411546086957 0.00224068041739130 0.00224724714782609 0.00225381565217391];
Diff = R2 - R1
Diff = 1×21
1.0e+-17 * 0.9975 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
plot(Diff);
Image Analyst
Image Analyst le 12 Fév 2022
@Muhammad Choudhury I presume you had two vectors that were definitely different and you plotted Diff and got all zeros, or think you did. It should look something like Benjamin's plot above. Did it not?
If not then you either had all zeros, and a very long vector, and the only-non-zero Diff element was the first one or last one so it essentially overlapped with the y axis so you didn't notice it.
OR you just looked at the command window and saw only zeros and though they were all zeros because the command window by default only prints the first 4 decimal places.
OR they actually were all zeros despite what you thought.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by