1×0 empty double row vector using find

30 visualizaciones (últimos 30 días)
Nadhynee
Nadhynee el 1 de Jun. de 2023
Editada: VBBV el 2 de Jun. de 2023
Hi, I have problem with this code:
clc; clear; close all
x=[0 0.1 0.2 0.3 0.4 0.5];
y=[1 7 4 3 5 2];
h=0.1;
n=(max(x)-min(x))/h
suma=0;
for i=2:n
aux=h*(i-1)
[row,col] = find(x==aux)
suma=suma+y(col);
end
when I run the for cicle and aux is equal to 0.3, the result of find is "1×0 empty double row vector", but there is a 0.3 in x. I'm really confused about this, someone can help me, please?
Thanks in advance.
  2 comentarios
Adam Danz
Adam Danz el 1 de Jun. de 2023
Editada: Adam Danz el 1 de Jun. de 2023
Nice list of references @Stephen23. The last link is broke but I think it points to this paper
I'll add this one

Iniciar sesión para comentar.

Respuestas (2)

James Tursa
James Tursa el 1 de Jun. de 2023
Editada: James Tursa el 1 de Jun. de 2023
Welcome to the world of floating point arithmetic. For your specific example, they are not equal. E.g.,
x=[0 0.1 0.2 0.3 0.4 0.5];
h=0.1;
i = 4;
aux=h*(i-1);
[row,col] = find(x==aux)
row = 1×0 empty double row vector col = 1×0 empty double row vector
fprintf('%20.18f\n',x(4))
0.299999999999999989
fprintf('%20.18f\n',aux)
0.300000000000000044
isequal(0.3,3*0.1)
ans = logical
0
You can see that these numbers are close but not exactly equal. They differ by one least significant bit in the floating point bit pattern:
num2hex(0.3)
ans = '3fd3333333333333'
num2hex(3*0.1)
ans = '3fd3333333333334'
To understand why you get this difference between 0.3 and 3*0.1, see this link:
It is usually bad practice to test for exact equality when floating point arithmetic is involved. Your code needs to be written to account for these small differences.

VBBV
VBBV el 1 de Jun. de 2023
clc; clear; close all
x=[0 0.1 0.2 0.3 0.4 0.5];
y=[1 7 4 3 5 2];
h=0.1;
n=(max(x)-min(x))/h
n = 5
suma=0;
for i=2:n
aux=h*(i-1)
[row,col] = find((x==round(aux,1)))
suma=suma+y(col);
end
aux = 0.1000
row = 1
col = 2
aux = 0.2000
row = 1
col = 3
aux = 0.3000
row = 1
col = 4
aux = 0.4000
row = 1
col = 5
  3 comentarios
John D'Errico
John D'Errico el 1 de Jun. de 2023
Be careful with rounding decimals. Even then, you do not get exactly what you think you get. For example,
x = 0.3;
sprintf('%0.55f',x)
ans = '0.2999999999999999888977697537484345957636833190917968750'
y = round(x,1);
sprintf('%0.55f',y)
ans = '0.2999999999999999888977697537484345957636833190917968750'
0.3 is not exactly representable in floating point arithmetic, and rounding will not change that fact.
VBBV
VBBV el 1 de Jun. de 2023
Editada: VBBV el 2 de Jun. de 2023
round function will output the same what we expect based on the number of input decimal precision given to the function. However, sprintf is different thing, which again displays outputs based on the input precision specified in the function
x = 0.3;
sprintf('%0.55f',x)
ans = '0.2999999999999999888977697537484345957636833190917968750'
y = round(x,1)
y = 0.3000
% round while displaying
sprintf('%0.9f',y)
ans = '0.300000000'
0.3 is not exactly representable in floating point arithmetic, and rounding will not change that fact.
Then what is the purpose of round function ?

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by