Skip to main content
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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

      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?