Unexpected result of the "find" command in Matlab

  • MATLAB
  • Thread starter hokhani
  • Start date
  • Tags
    Matlab
In summary: 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...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.
  • #1
hokhani
504
8
The program below gives "error=4 7" unexpectedly while all the arrays of "real" are 1! I would be grateful for any help.

Code:
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)
 
Physics news on Phys.org
  • #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?
 
  • #3
HuskyNamedNala said:
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?

Thank you. What should I do to get rid of this problem?

The output of real and error are:
Code:
real =

    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000error =

     4     7
 
  • #4
HuskyNamedNala said:
I've never seen this error.
I tested it in another computer and got again to those unexpected values ,[4 7], for "error".
 
  • #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.
 
  • #6
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:
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:
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:
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
 
  • Like
Likes hokhani and HuskyNamedNala
  • #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
 
  • #8
HuskyNamedNala said:
... 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.

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:
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:
real(a)

ans =

     1
 

FAQ: Unexpected result of the "find" command in Matlab

What does the "find" command in Matlab do?

The "find" command in Matlab is used to locate the indices of array elements that meet a specified condition. It returns a vector of indices where the condition is met.

Why am I getting unexpected results when using the "find" command in Matlab?

There could be several reasons for unexpected results when using the "find" command in Matlab. Some common reasons include incorrect syntax, using the wrong variable or array, or not specifying the correct condition. It is important to double-check your code and ensure that the condition you are using is appropriate for the data you are searching for.

Can I use the "find" command in Matlab to search for multiple conditions?

Yes, the "find" command in Matlab can be used to search for multiple conditions by using logical operators such as AND (&) and OR (|). For example, to find elements that meet both condition A and B, you would use "find(A & B)".

Is there a way to speed up the "find" command in Matlab for large arrays?

Yes, there are several ways to optimize the "find" command in Matlab for large arrays. One way is to use logical indexing instead of the "find" command, which can be faster for certain conditions. Another way is to preallocate memory for the output vector before using the "find" command. Additionally, using vectorization and avoiding loops can also improve the speed of the "find" command.

Can the "find" command in Matlab be used for non-numeric data?

Yes, the "find" command in Matlab can be used for non-numeric data as long as the condition being searched for can be evaluated with logical operators. For example, you could use the "find" command to search for all elements in a cell array that contain a specific string.

Similar threads

Replies
10
Views
2K
Replies
1
Views
3K
Replies
10
Views
2K
Replies
4
Views
3K
Replies
4
Views
5K
Replies
1
Views
2K
Replies
2
Views
3K
Replies
4
Views
1K
Back
Top