Is precision the reason my code doesn't work ?
Show older comments
I have a matrix (as attached) I try to find rows that has a specific number in the 2nd column as N. It works when try N=1 or N=0 and extract the the new data from the original DATA. But when I pick a number from column 2 other than 1 or 0 For example the number in DATA(3044,2) which is (0.123945562585054) the code doesNOT work. I'm wondering if it is the precision or what ? Any help is highly appreciated:
data=[];
N=1;
for j=1:length(DATA);
if DATA(j,2)==N;
data =[data;DATA(j,:)]
else
data=[data];
end
end
Accepted Answer
More Answers (1)
Jan
on 16 Sep 2017
Note that the statement
data=[data];
is simply a confusing waste of time: It uses the operator for concatenating a list of elements [] for one element only and assigns the output to the same variable as the input. 1. omit the unnecessary square brackets and 2. omit the complete line, when it does not perform anything.
The iterative growing of arrays consumes a lot of resources. If the final output contains 1000 rows, Matlab must allocate sum(1:1000) rows to create it: 500'500. A better implementation without a loop and without the iterative growing:
data = DATA(DATA(:, 2) == N, :)
The actual problem of the precision has been explained by Walter already. This might be useful:
match = abs(DATA(:, 2) - N) < 10 * eps(N);
data = DATA(match, :);
The limit might be absolute or relative depending on the meaning of your data. The factor 10 is chosen arbitrarily here also and has to be adjusted.
3 Comments
Faez Alkadi
on 16 Sep 2017
Edited: Faez Alkadi
on 16 Sep 2017
Data = round(Data, 4)
Note that this will not solve the precision problem! The elements of Data are still double precision values stored in binary format. Many decimal numbers do not have an exact binary representation, and therefore even the rounded numbers are not guaranteed to have only zeros as trailing digits. Example:
x1 = 0.5347 + 0.0001;
x2 = 0.7323 + 0.0001;
sprintf('%.16g\n', round(x1, 4), round(x2, 4))
% 0.5348000000000001
% 0.7323999999999999
And this is the exact and correct result for double precision data.
x1 == 0.5348 % FALSE !!!
There is no way around using an interval for the comparison when fractional numbers are used. But you can use integer values instead:
Data = round(Data * 10000);
As long as the original Data is smaller than 900'719'925'474 (2^53 / 10000), the results are exactly integers and the comparison works without an interval:
x1 = 0.5347
x2 = x1 + 0.0001
round(x1 * 10000) == 5347 % TRUE
round(x2 * 10000) == 5348 % TRUE
Walter Roberson
on 16 Sep 2017
For N digits after the decimal place, at most 2^N out of 10^N values have exact binary representation -- for example for 3 decimal places, at most 8 out of 1000 values have exact binary representation.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!