# Using "find" for finding decimal values

조회 수: 63 (최근 30일)
MiauMiau . 2012년 12월 13일
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

댓글을 달려면 로그인하십시오.

### 채택된 답변

Matt Fig 2012년 12월 13일
편집: Matt Fig 님. 2012년 12월 13일
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))
I have a feeling you will need to set your tolerance much higher, like 10^-4.
##### 댓글 수: 4표시 이전 댓글 수: 3숨기기 이전 댓글 수: 3
Matt Fig 2012년 12월 14일
I found out by looking at how close your guess value was to the actual value. You may have to do some tweaking but you get the idea now.

댓글을 달려면 로그인하십시오.

### 추가 답변 (4개)

Azzi Abdelmalek 2012년 12월 13일
Try
idx = find(abs(X(:,1 )-17.9203)<eps)
##### 댓글 수: 2표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
Kye Taylor 2012년 12월 13일
should be <=

댓글을 달려면 로그인하십시오.

Jan 2012년 12월 13일
편집: Jan 님. 2012년 12월 13일
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표시 이전 댓글 수: -1숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

MiauMiau 2012년 12월 13일
I was not aware of that, that is pretty horrible! Anyway, both of your suggestions did not work, so also for:
idx = find(abs(X(:,1 ) - 17.9203) < eps(17.9203))
I did get an empty matrix returned - although the value is to be found in X. Also, when I changed the number a bit, I then got too many answers returned. But what strikes me most, is that in the first case, I just did not get any answer at all!..?!
##### 댓글 수: 7표시 이전 댓글 수: 6숨기기 이전 댓글 수: 6
Jan 2012년 12월 13일
@Azzi: I've stressed this detail, because the OP seems to be confused about this topic already. I did not assume, that you struggle with floating point arithmetics.

댓글을 달려면 로그인하십시오.

MiauMiau 2012년 12월 13일
I am really sorry for my use of the word "horrible".
If you meant the following command though ( I have used another nummerical value):
idx = find(abs(X(:,1 ) - 6.0018) <= eps(6.0018))
I - again - got an empty 0,1-matrix.
Also, I was not criticising the obviously necessary restriction on measurement and computation precision but I was amazed that there is not a simpler command to handle that. Anyway. What shall I try next?
##### 댓글 수: 1표시 없음숨기기 없음
Jan 2012년 12월 13일
Why do you assume that 6.0018 is an element of X?

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!