Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Unexpected result of the "find" command in Matlab

  1. Feb 19, 2015 #1
    The program below gives "error=4 7" unexpectedly while all the arrays of "real" are 1! I would be grateful for any help.

    Code (Text):

    kky=pi/(2*10000):pi/10000:1/658;
        ky=[-fliplr(kky) kky];
            kx=sqrt((1/658)^2-ky.^2);
    expiphi_1=(658/1)*(kx+i*ky);
    real=expiphi_1.*conj(expiphi_1)
    error=find(real~=1)
     
  2. jcsd
  3. Feb 19, 2015 #2
    I've never seen this error. You could either find when real==1 then compute the complement or write a simple search loop yourself and see what you get. What is the output of the "real" vector?
     
  4. Feb 20, 2015 #3
    Thank you. What should I do to get rid of this problem?

    The output of real and error are:
    Code (Text):

    real =

        1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000


    error =

         4     7
     
  5. Feb 20, 2015 #4
    I tested it in another computer and got again to those unexpected values ,[4 7], for "error".
     
  6. Feb 20, 2015 #5
    Try writing your own find loop. Also, check the data type for "real". If I remember correctly real is a command in MATLAB too, though I've since switched to Python.
     
  7. Feb 23, 2015 #6

    kreil

    User Avatar
    Gold Member

    The problem here is due to the nature of floating point arithmetic, where there can be roundoff errors in trying to represent numbers. You are comparing some floating point numbers for exact equality to a number. They are not exactly equal in a bit-for-bit sense, because the numbers came about as the result of arithmetic operations. So this type of comparison is discouraged.

    Here is some proof, since if you subtract 1 from the array you don't get all zeros:

    Code (Text):

    real-1

    ans =

       1.0e-15 *

      Columns 1 through 4

                       0                   0                   0  -0.111022302462516

      Columns 5 through 8

                       0                   0  -0.111022302462516                   0

      Columns 9 through 10
                       0                   0
     
    Inspecting the 3rd and 4th elements to 20 digits:

    Code (Text):
    sprintf('%.20f',real(3))

    ans =

    1.00000000000000000000

    sprintf('%.20f',real(4))

    ans =

    0.99999999999999989000
    So what you need to do to avoid this problem is use a comparison tolerance. Something like the following:

    Code (Text):
    tol = eps;
    error = find(abs(real-1)>tol)

    error =

       Empty matrix: 1-by-0

    Future reading:
    http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
     
  8. Feb 23, 2015 #7
    Ahh, good call. I remember running into a similar error when I was writing an optimization code and the search vector kept returning 0. It turns out it was ~10^-10
     
  9. Feb 23, 2015 #8

    kreil

    User Avatar
    Gold Member

    This is a good point, because "real" is a MATLAB function that returns the real part of a complex number. In general you shouldn't use names that override MATLAB functions, so just call this array something else, like "real1", "real_matrix" or "result".

    If you were to execute the original code and then try to use the MATLAB real function, you might be disappointed that it no longer works:

    Code (Text):
    a = 1- 5i

    a =

      1.000000000000000 - 5.000000000000000i

    real(a)
    Subscript indices must either be real positive integers or logicals.
    If you clear the variable, the MATLAB function works again:


    Code (Text):
    real(a)

    ans =

         1
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Unexpected result of the "find" command in Matlab
Loading...