The FIND command returns an empty matrix for a number I know exists

46 Ansichten (letzte 30 Tage)
Dharmesh Nadhe
Dharmesh Nadhe am 28 Jan. 2011
Kommentiert: Image Analyst am 16 Apr. 2018
I have sets of data in column vector form (around 70000 elements).
To find the maximum element I used "max(x)" and the value comes to around 0.0305.
I want to know the indices of this element (number of the max value element). I am using find command as
find(x==0.0305)
But I get answer as
"Empty matrix: 0-by-1".
It works when I use
find(x<0.0305)
and displays all indices with value less than 0.0305. Can someone explain what I am doing wrong?
  1 Kommentar
Sanjay
Sanjay am 22 Okt. 2013
I have a similar related problem. Above given question speaks about a unique problem where the indices of the maximum element has to be find. I have a data series contained in 'am1' with sample rate of 100 sps. The time 't' is of 359.79 secs so in total i have 35979 data points.
In my code I am using
trScl = find(t ==183.5); trScu = find(t ==213.5); where these index point have to be used to create a new time series
Sc=am1(trScl:trScu);
there is no error but output comes with an empty matrix likewise; trScl [] trScu []
Now if instead of above mentioned values I use trScl = find(t ==80); trScu = find(t ==98);
the code works providing me with the correct answer. i.e t <= 100 it works,!!!
Kindly help me on this subject

Melden Sie sich an, um zu kommentieren.

Antworten (5)

Doug Eastman
Doug Eastman am 28 Jan. 2011
The issue here is that the value is not exactly 0.0305, it gets truncated when displayed in the command window. You need to save the maximum value in a variable and then use that variable to find the indices:
myMax = max(x);
i = find(x==myMax);
  1 Kommentar
Jiro Doke
Jiro Doke am 28 Jan. 2011
I recommend this answer. By saving the actual max value, you will get the full precision. Using the
[C,I] = max(...)
syntax for a vector will only give you the index of the first occurrence, not all occurrences.

Melden Sie sich an, um zu kommentieren.


Oleg Komarov
Oleg Komarov am 28 Jan. 2011
Look at second output:
[C,I] = max(...)
Oleg
  1 Kommentar
Michelle Hirsch
Michelle Hirsch am 28 Jan. 2011
I recommend this answer - it gets right to the desired solution, with no worries about numeric precision.

Melden Sie sich an, um zu kommentieren.


Todd Flanagan
Todd Flanagan am 28 Jan. 2011
You are bumping into issues with floating point accuracy. Loren has a nice blog post about accuracy with floating point numbers.
You can get the result directly from the MAX command using the form that returns both the numerical and index results:
[C, I] = max(x);
Note that with [C,I] = max(...) if there are several identical maximum values, the index of the first one found is returned.
If you want to use find or need to use find because you expect more than 1 occurrence of the maximum, you'll need to take some approach that either brackets the number you are looking for or programatically gives you the exact floating point representation you are looking for.
For example:
a = [1 2 3 .3501/10 4];
myMin = min(a)
find(a == myMin)
or for other sorts of problems you may want to bracket like:
find(a<.0351 & a>.0350)

Image Analyst
Image Analyst am 22 Okt. 2013
  2 Kommentare
Sanjay
Sanjay am 9 Dez. 2013
Bearbeitet: Sanjay am 9 Dez. 2013
THANK YOU, Someway it was helpful but still the problem is not yet solved. !!!
Image Analyst
Image Analyst am 9 Dez. 2013
I see. Dharmesh has not accepted any of the suggested answers, so perhaps he does not consider it solved. He should accept one or ask a follow up question. If Sanjay has a question, he should start his own discussion in a separate thread.

Melden Sie sich an, um zu kommentieren.


omar kammouh
omar kammouh am 15 Apr. 2018
I had the same problem. I solved it by simply copying and then pasting the column (or the matrix) from which I am trying to find the index of a value.
  1 Kommentar
Image Analyst
Image Analyst am 16 Apr. 2018
The function you should use now to compare floating point numbers is called ismembertol().

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by