sum() missing for certain values
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I have an issue which i do not understand using the basic function sum(). I have an array with 2 million values consisting of doubles from 0 to 1 with 3 decimals (rounded). I want to display the distribution by counting every possible output except zero and one using the sum function (0.001-0.999). I tried to calculate it with and without a loop but im allways missing the sum for certain values either way. Example code below.
Vector=rand(2000000,1);
Vector=round(Vector,3,"decimals");
numbers=0.001:0.001:0.999;
b=1;
distribution2=double.empty(999,0);
for x=0.001:0.001:0.999
distribution2(b)=sum(Vector==x);
b=b+1;
end
figure()
plot(numbers,distribution2)
title('distribution2 (with loop)')
distribution(:)=sum(Vector==numbers);
figure()
plot(numbers,distribution)
title('distribution')


As u can see I get a feedback with different gaps using the calculation methods as shown above. I checked the gaps (e.g. 0.7) by using the sum function inside the command window and got back a positive feedback
>> sum(Vector==0.7)
ans =
2009
Can someone explain to me why I am having issues calculating the sum for all numbers and why I am getting two different results depending on how I am trying to calculate it?
Thank you and nice Regards
Anton
4 comentarios
Stephen23
el 20 de Feb. de 2023
Editada: Stephen23
el 20 de Feb. de 2023
"I'm aware that matlab is not able to display exactly 0.7 due to binary flaoting numbers"
MATLAB certainly can "display exactly 0.7", even binary floating point. Lets try it right now:
format short G
0.7
"i was not aware that matlab is using different construction for 0.7 inside command window and inside code"
I have never heard that MATLAB uses a "different construction for 0.7 inside command window and inside code", and I strongly doubt that such a "different construction" exists due solely to where the value is defined. I tried it in R2018a:

The two values are exactly the same.
The links I posted earlier introduce floating point number behavior. David Goldberg's article is highly recommended.
Respuesta aceptada
KSSV
el 20 de Feb. de 2023
Don't use
sum(Vector==x)
use
tol = 0.0001 ; % set your acceptable tolerance
sum(abs(Vector-x)<tol) ;
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!