Skip to main content
Library homepage
 
Engineering LibreTexts

7.10: The find() function

  • Page ID
    87696
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

      By Carey Smith

      The MATLAB built-in function find() is an efficient method to find the indices of data that satisfy some logical condition.

      The syntax of the find function is:

      indx = find( condition )

      The output of the find() function is the set of indices for which the "condition" is true.

      This is illustrated by these examples:

      Example \(\PageIndex{1}\) find() with an == condition

      a = 1:0.5:4 % a = [1.0 1.5, 2.0, 2.5, 3.0, 3.5, 4.0]
      idx1 = find(a == 3.0) % Find the elements equal to 3 and return their indices
      % idx1 = 5, becuz the 5th element of a is 3.0

      % Check it with this code
      a_idx1 = a(idx1) % = 3.0

      Solution

      Add example text here.

      .

      Example \(\PageIndex{2}\) find() with a < condition

      a = 1:0.5:4 % Same vector as above
      idx2 = find(a < 3.0)
      % idx = 1 2 3 4
      % These are the indices of the elements of a < 3

      % Check it with this code
      a(idx2) % = 1.0, 1.5, 2.0, 2.5

      Solution

      Add example text here.

      .

      Example \(\PageIndex{3}\) find() with a compound condition

      a = 1:0.5:4 % Same vector as above
      % Find the indices of the elements between 2.0 and 3.5, inclusive
      % We need to use a compound conditional expression
      idx3 = find( (2.0 <= a) & (a <= 3.5) )
      % "&" = "and"
      % That means that for a particular value of a, both conditional parts have to be true for
      % for the compound conditional to be true.
      % idx3 = 3 4 5 6
      % These are the indices of the elements that satisfy both parts

      % Check it with this code

      a(idx3) % = 2.0, 2.5, 3.0, 3.5

      Solution

      Add example text here.

      .

      Example \(\PageIndex{4}\) The result of the compound condition itself

      %% The condition by itself:
      ( (2.0 <= a) & (a <= 3.5) )
      %ans =
      % 1×7 logical array
      % 0 0 1 1 1 1 0
      % 0= False for the elements that do not satisfy both parts of the
      % conditional
      % 1=True for the elements that do satisfy both parts

      Solution

      Add example text here.

      .

      Example \(\PageIndex{5}\) What could go wrong?

      %% What happens if we try this?
      % A single condition, instead of a compound condition:
      idx3b = find(2.0 <= a <= 3.5)

      % The result is: idx3b = 1 2 3 4 5 6 7 (!?)
      % This is clearly not what was desired.

      % The condition by itself (2.0 <= a <= 3.5) returns:
      % 1 1 1 1 1 1 1
      % becuz every value of a is either >= 2 or <= 3.5
      % You can only have a comparison between 1 number and 1 variable
      % or 2 variables.
      % You need to have each part of the compound be a complete condition by itself, as above, namely:

      % ( (2.0 <= a) & (a <= 3.5) )

      Solution

      Add example text here.

      .

      Example \(\PageIndex{6}\) find() with 2 variables

      a = 1: 0.5 :4 % As above
      b = 2
      idx4 = find(b == a)
      % idx4 = 3
      a(idx4) % 2

      Solution

      Add example text here.

      .

      Example \(\PageIndex{7}\) find() with no match.

      When there are no elements that satisfy the condition,

      then the find() command returns an empty vector.

      a = 1:0.5:4
      b = 20
      idx4 = find(b == a)

      % idx4 = empty

      Solution

      Add example text here.

      .

      Example \(\PageIndex{8}\)

      %% Example 5: 2 vectors of the same length
      a = 1:0.5:4
      % a = [1.0 1.5, 2.0, 2.5, 3.0, 3.5, 4.0]
      c = [ 0 1 2 3 4 5 6 ]
      idx5 = find( a == c)
      % idx5 = 3, because a(3) = b(3) = 2
      % Also, even though both vectors have values of 1, 3, and 4
      % these don't occur in the same place, so they are not matches.

      Solution

      Add example text here.

      .

      Exercise \(\PageIndex{1}\) Test Scores >= 90

      % Use these test scores:
      test_scores = [
      73
      72
      88
      94
      77
      92
      88
      65
      86
      78
      97
      65]
      % Use the find() function to find and list the test scores >= 90

      Answer

      Add texts here. Do not delete this text first.

      .


      This page titled 7.10: The find() function is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.

      • Was this article helpful?