Is precision the reason my code doesn't work ?
4 views (last 30 days)
Faez Alkadi on 15 Sep 2017
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:
Walter Roberson on 15 Sep 2017
Yes, precision is the problem. You have to take special steps to be able to see the full precision of a number in order to be able to type it in with enough precision that you can do an exact comparison using "==" like you do.
I recommend that you use https://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact--exact-version-of-num2str- to look at what the numbers really stored are.
You should consider whether you really want to do exact bit-for-bit comparisons. Any two ways of computing values that are algebraically equivalent can have different round-off, so you should only ever use == to compare values that have been extracted from the same array. Otherwise you should compare with a tolerance.
More Answers (1)
Jan on 16 Sep 2017
Note that the statement
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.