# Using "find" for finding decimal values

83 views (last 30 days)

Show older comments

Hi

I use the matlab command importdata:

X = importdata('filename.csv');

to read in a csv file with three columns.

Now, for finding a specific values in the matrix X, I simple use the find command as follows:

idx = find(X(:,1 ) == 17)

But, the same seems not to be possible for decimal numbers. This for instance would not work:

idx = find(X(:,1 ) == 17.9203)

even though 17.9203 is to be found in the original csv file. What is the problem and what can I do?

Thanks

##### 0 Comments

### Accepted Answer

Matt Fig
on 13 Dec 2012

Edited: Matt Fig
on 13 Dec 2012

MiauMiau, the numbers you are looking for are obviously different from the short format you see. Do this:

[~,idx] = min(abs(X(:,1) - 6.0018));

Y = X(idx,1);

[~,idx] = min(abs(X(:,1) - 17.9203));

fprintf('%15.15f %15.15f\n',Y,X(idx,1))

And show us the ouput in a comment on this answer, don't add another answer!

I have a feeling you will need to set your tolerance much higher, like 10^-4.

##### 4 Comments

Matt Fig
on 14 Dec 2012

### More Answers (4)

Jan
on 13 Dec 2012

Edited: Jan
on 13 Dec 2012

There is no exact representation of decimal floating point numbers in binary format for all values. You find a lot of corresponding discussion in this forum:

0.1 + 0.2 - 0.3 == 0

>> false

This is no bug, but the expected behaviour, when floating point numbers are represented with a limited precision.

A consequence is, that you cannot compare numbers like 17.9203 and 17.92029999999999999999 sufficiently and even the display in the command window can be rather confusing.

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!