scalar matrix operations / vector arithemtics

Hello!
I have a problem when I want to calculate percentage deviations from values. Ie. I have the following vecotrs:
a = [-3.67843750000000;
-0.149062500000000;
-0.0284375000000000;
1.52562500000000;
0.217812500000000;
0.00562500000000000;
-0.00625000000000000;
-0.00875000000000000;
-0.00750000000000000;
-0.00937500000000000;
-0.00937500000000000;
-0.00937500000000000]
a = 12×1
-3.6784 -0.1491 -0.0284 1.5256 0.2178 0.0056 -0.0063 -0.0088 -0.0075 -0.0094
b = [-5;
-0.0116649326387481;
-2.72141306933060e-05;
4.98833506736125;
0.255995664691759;
0.0123648464061628;
0.000597234436888179;
2.88470200832711e-05;
1.39333989516825e-06;
6.72997092199941e-08;
3.25064320400358e-09;
1.57009314931713e-10]
b = 12×1
-5.0000 -0.0117 -0.0000 4.9883 0.2560 0.0124 0.0006 0.0000 0.0000 0.0000
When I want to see on how much percentage each element in vector a differs from the same index element in vector b, i thought I can do this with a for loop:
for i=1:1:length(a)
deviation(i,1) = (a(i,1) - b(i,1))*100/b(i,1);
end
deviation
deviation = 12×1
1.0e+09 * -0.0000 0.0000 0.0001 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0005 -0.0139
but as you can see the result is wrong, or I have formated it wrong.
I then thought ok, maybe i can do it with scalar multiplication and division:
deviation_scalar = (a - b)*100./b
deviation_scalar = 12×1
1.0e+09 * -0.0000 0.0000 0.0001 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0005 -0.0139
but this is also not returning the correct result. I.e. for the first entry the calculation would be:
(-3.6784 - -5)*100/-5
ans = -26.4320
which gives me now the correct result. But i don't understand what I do wrong with the vector arithmetic. Does anyone have a clue?

 Réponse acceptée

Stephen23
Stephen23 le 14 Déc 2022
Modifié(e) : Stephen23 le 15 Déc 2022
"but as you can see the result is wrong.."
No, I do not see that. I see the default format which has four digits after the decimal point and a common multiplier.
"...or I have formated it wrong."
Well, the default format SHORT uses four digits after the decimal point and a common multiplier:
Question: what is -26 to the nearest 1e9 (the common multiplier), when displayed with just four fractional digits?
Answer: -0.0000 * 1e9
And that is exactly what MATLAB displays. If you want, you can change the FORMAT():
a = [-3.6784375;-0.14906250;-0.0284375;1.525625;0.2178125;0.005625;-0.00625;-0.00875;-0.0075;-0.009375;-0.009375;-0.009375]
a = 12×1
-3.6784 -0.1491 -0.0284 1.5256 0.2178 0.0056 -0.0063 -0.0088 -0.0075 -0.0094
b = [-5;-0.0116649326387481;-2.72141306933060e-05;4.98833506736125;0.255995664691759;0.0123648464061628;0.000597234436888179;2.88470200832711e-05;1.39333989516825e-06;6.72997092199941e-08;3.25064320400358e-09;1.57009314931713e-10]
b = 12×1
-5.0000 -0.0117 -0.0000 4.9883 0.2560 0.0124 0.0006 0.0000 0.0000 0.0000
deviation_scalar = (a - b)*100./b
deviation_scalar = 12×1
1.0e+09 * -0.0000 0.0000 0.0001 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0005 -0.0139
format long G
deviation_scalar % exactly the same numbers, different precision and multiplier
deviation_scalar = 12×1
1.0e+00 * -26.43125 1177.8685022565 104395.345893944 -69.4161482859845 -14.9155512995639 -54.5081288094575 -1146.49022460341 -30432.4224642333 -538374.976982149 -13930323.6349259
format long
deviation_scalar % exactly the same numbers, different precision and multiplier
deviation_scalar = 12×1
1.0e+09 * -0.000000026431250 0.000001177868502 0.000104395345894 -0.000000069416148 -0.000000014915551 -0.000000054508129 -0.000001146490225 -0.000030432422464 -0.000538374976982 -0.013930323634926
format short E
deviation_scalar % exactly the same numbers, different precision and multipliers
deviation_scalar = 12×1
1.0e+00 * -2.6431e+01 1.1779e+03 1.0440e+05 -6.9416e+01 -1.4916e+01 -5.4508e+01 -1.1465e+03 -3.0432e+04 -5.3837e+05 -1.3930e+07
These are all the same numbers, just displayed to different precisions and exponents.

5 commentaires

Dear Stephen23,
thanks for your answer. But to be true, I don't understand how -0.0000 x 1.0 e+9 is the same as -26. And why do I get different result when use slightly different solution ways.
With this logic a lot of different numbers would get me the same -0.0000 x 1.0e+9 result and this would be catastrophic.
How do I know when matlab will make the "correct" format assumption as with (-3.6784 - -5)*100/-5 and the "not correct" format assunmption? I believe there is a logic to this but this will get me in the responsibility to check for the correct format.
Btw, why is my 20 year old calculator showing me a "correct", so with a meaningful format, result? But one of the powerful mathematical softwares not?
Off course, one might argue it's me who has to know what he's been doing, but if I do experiments where I don't know what results are to expected, I definetely don't want to question every mathematical calculation MATLAB does for me.
Ok, I think I misunderstood, that this affects only the displaying format of numbers. So MATLAB - internally - does allways have the correct calculation result. So its up to me to decide what displaying format I like and force matlab to use it.
Hey Stephen23,
thanks again for your efforts on explaining matters to me and your examples. I think I get the point. I have found that format shortG gives me my "correct" expectation of values.
What I found not intuitive is that MATLAB used a common multiplier to display elements in a vector, which is - in my opinion - not suitable if you have values that differ in several magnitudes. So I would have found it intuitive if MATLAB would have used the shortG formating to show me values in a - for me - reasonable way.
Steven Lord
Steven Lord le 15 Déc 2022
If you want to change the default display format for floating-point numbers you can do that in the Preferences. See the "Format Floating-Point Numbers" section on this documentation page for instructions.
Thanks for the hint and help!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by