MATLAB: using the find function to get indices of a certain value in an array

前端 未结 3 2034
面向向阳花
面向向阳花 2021-01-16 21:28

I have made an array of doubles and when I want to use the find command to search for the indices of specific values in the array, this yields an empty matrix which is not w

3条回答
  •  有刺的猬
    2021-01-16 21:49

    By default, each element of a numerical matrix in Matlab is stored using floating point double precision. As you surmise in the question format short and format long merely alter the displayed format, rather than the actual format of the numbers.

    So if y1 is created using something like y1 = rand(100, 1), and you want to find particular elements in y1 using find, you'll need to know the exact value of the element you're looking for to floating point double precision - which depending on your application is probably non-trivial. Certainly, peaks=find(y1==0.8236) will return the empty matrix if y1 only contains values like 0.823622378...

    So, how to get around this problem? It depends on your application. One approach is to truncate all the values in y1 to a given precision that you want to work in. Funnily enough, a SO matlab question on exactly this topic attracted two good answers about 12 hours ago, see here for more.

    If you do decide to go down this route, I would recommend something like this:

    a = 1e-4 %# Define level of precision
    y1Round = round((1/a) * y1); %# Round to appropriate precision, and leave y1 in integer form
    Index = find(y1Round == SomeValue); %# Perform the find operation
    

    Note that I use the find command with y1Round in integer form. This is because integers are stored exactly when using floating point double, so you won't need to worry about floating point precision.

    An alternative approach to this problem would be to use find with some tolerance for error, for example:

    Index = find(abs(y1 - SomeValue) < Tolerance);
    

    Which path you choose is up to you. However, before adopting either of these approaches, I would have a good hard look at your application and see if it can be reformulated in some way such that you don't need to search for specific "real" numbers from among a set of "reals". That would be the most ideal outcome.

    EDIT: The code advocated in the other two answers to this question is neater than my second approach - so I've altered it accordingly.

提交回复
热议问题