This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 7 years ago.
I wrote the following code. The problem is that at the specific value of 0.288,
T turns to zero for no obvious reason. Any explanation? The weird thing is that when I change it to 0.28 it works fine.
time=(0:0.002:0.560);
time_f=reshape(time,281,1);
time1=0;
time2=0;
for i=1:1
for j=1:281
T=time_f(j,i);
if (i==1) && (T==0.288);
time1=T;
end
end
end
If you test the code you will find that the time1 value will be zero and if you change T to 0.28 it will work.
The answer is simple
>> time_f(time_f == 0.288)
ans =
Empty matrix: 0-by-1
Your matrix doesn't contain the value 0.288
This is due to float precision, so instead of being 0.288, your value is 0.287999999 for example.
Just use roundn, which round to a specified number of significant digits
>> time_f = roundn(time_f,-3);
>> find(time_f == 0.288)
ans =
145
If you don't have Mapping toolbox you can do
>> time_f = round(time_f * 1000) / 1000
You are using floating point arithmetic, which has certain precision errors. Your matrix does not contain exactly the value 0.288.
Related
This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I have MATLAB code
clear all;
clc;
t=0:0.001:0.1;
N=length(t);
for n=1:N
if t(n)==0||t(n)==0.02||t(n)==0.04||t(n)==0.06||t(n)==0.1
fprintf('%5.5f\n',t(n));
end
end
The result is
Why if t(n)==0.06, in my command window cannot displaying 0.06?
Does my MATLAB have an error? I'm using MATLAB R2014b
It's a numerical error because the 0.001 increments will not be precisely 0.001 under the hood.
The same thing happens in 2020b:
>> t = 0:0.001:0.1;
>> t(61)
ans =
0.0600
>> t(61) == 0.06
ans =
logical
0
If you want to compare floats, use some kind of tolerance like eps instead of ==:
>> (t(61) - 0.06) < eps
ans =
logical
1
This question already has answers here:
Small negative number maintains sign after rounding
(2 answers)
Closed 3 years ago.
Why am I getting the following in MATLAB? Tried in 2015b and 2019a and same result.
sprintf('%.15g',-1*0)
ans =
'-0'
format hex shows you what's going on - IEEE double precision numbers can have negative zero.
>> format hex
>> 0
ans =
0000000000000000
>> -0
ans =
8000000000000000
This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 3 years ago.
I got a problem about checking if two matrices are equal in MATLAB.
Specifically, I want to verify if, for a matrix W, all elements are equal to each other and all row sums are equal to 1 we would have W = W^2.
Therefore I wrote the following code which aims to check if every element of these two matrices are equivalent, to conclude whether the matrices are equal to each other. But it turns out that this does not work since the W8 matrix should be equal to its square.
for i = 1 :60
for j = 1 :60
if(W8(i,j) - W8_square(i,j) ~= 0)
disp('the matrix are not equal');
break;
end
end
end
There is a matlab function for it:
eq = isequal(W8,W8_square) should work
Here you find the reference
https://www.mathworks.com/help/matlab/ref/isequal.html
Be careful that if this checks for EXACT identity, so computation errors, of the order of eps, may affect the result.
To solve this, I would subtract the two matrixes and check the norm of the result: below a certain threshold (low) they are equal.
Here you have an example code for your problem:
n = 10; %matrix size
W8 = ones(n)/n; %Generating W8
W8_square = W8^2;
eq = isequal(W8,W8_square) %checking EXACT identity
M_difference = W8-W8_square; %Difference matrix
eq2 = isequal(M_difference<=eps,ones(n)) %%comparing every value with eps
This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 6 years ago.
I got exactly this error:
Attempted to access E(3,1); index must be a positive integer or logical.
But the index is E(3,1), those numbers are both positive. What is going on?
for t=T:0.2:4
for i=1:N
% D = D +1
x = randi(Nsamples,1,1);
if(x==1)
Etemp = E(t*5,i) - S(x)*S(x+1) + (-S(x))*S(x+1);
elseif(x==Nsamples)
Etemp = E(t*5,i) - S(x)*S(x-1) + (-S(x))*S(x-1);
else
%********************* This is the error line
Etemp = E(t*5,i) - (S(x-1)*S(x)+S(x)*S(x+1))+ (S(x-1)*(-S(x))+(-S(x))*S(x+1));
end
end
end
3 index in E(3,1) may not exactly be an integer. In your case, index row index 3 is generated by multiplying t*5 i.e. 0.6*5 (if t=0.6). It does not guarantee it to be an integer.
In a high accuracy check on generated index value 3, you will find that it is off from the exact integer 3 by 1 bit or so at its least significant end.
Therefore, while indexing E(3,1), 3 is not perceived as an integer.
In cases where you generate the index by multiplying with a decimal, make sure to convert it to int before using it for indexing such as round(t*0.5) or int8(t*0.5).
Or all together avoid index which are generated by multiplying the decimals.
This question already has answers here:
What are the best practices for floating-point comparisons in Matlab?
(5 answers)
Closed 8 years ago.
I need to compare two double values in Matlab and check if they are equal. Now the two compared values are displayed to the user, so he can check the printed result, if necessary.
Now I need to know: Is it possible to compare the two double values so that they are equal, if their decimal representation (using 15 significant digits) is equal? For performance reasons, I would prefer not to compare the resulting strings.
For example the two hexadecimal values 3fd04b2bcf617348 and 3fd04b2bcf617359 represent the same displayed number and should therefore be treated equal, whereas 3fd04b2bcf617348 and 3fd04b2bcf617347 have different decimal representations and should be treated as different, even if their difference is lower:
fprintf('eq: %.15g\n', hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617359'))
fprintf('ne: %.15g\n', hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617347'))
You can round the values created by hex2num to 15 digits. This is done using the round function.
For MATLAB R2014b and higher, you can specify the precision directly
b = round(a,15);
For older versions, round just rounds to integers, so you will have to do it manually:
b = round(10^15 .* a) ./ 10^15;
If you compare the numbers after rounding, you get the desired behaviour:
a = [ hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617359') ; ...
hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617347') ];
% Round to 15 digits
b = round(10^15 .* a) ./ 10^15;
% Compare results
abs(b(1,1) - b(1,2)) < 4*eps(b(1,1))
ans =
1
abs(b(2,1) - b(2,2)) < 4*eps(b(2,1))
ans =
0