7.10: The find() function (2024)

  1. Last updated
  2. Save as PDF
  • 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.

      .

      7.10: The find() function (2024)

      FAQs

      How do you use the Find () function? ›

      Syntax
      1. FIND(find_text,within_text,start_num)
      2. Find_text is the text you want to find.
      3. Within_text is the text containing the text you want to find.
      4. Start_num specifies the character at which to start the search. The first character in within_text is character number 1. If you omit start_num, it is assumed to be 1.

      What does the find function do in Excel? ›

      What is the FIND Function? The FIND Function[1] is categorized under Excel TEXT functions. The function will return the position of a specified character or sub-string within a supplied text string. In financial analysis, we can use this function to locate specific data.

      What does find() do in Python? ›

      The Find function in Python is a string method in Python used for searching a substring within a given string. The method returns the index of the first occurrence of the specified substring, or -1 if the substring is not found.

      What is the use of find () method in string? ›

      The find() method in Python is used to locate the index of a substring within a string. It returns the index of the first occurrence of the substring, or -1 if the substring is not found. This method is case-sensitive by default, but we can handle case sensitivity using the appropriate parameters.

      What does find the function mean? ›

      Evaluating a function means finding the value of f(x) =… or y =… that corresponds to a given value of x. To do this, simply replace all the x variables with whatever x has been assigned. For example, if we are asked to evaluate f(4), then x has been assigned the value of 4. Example: Given that f(x) = 3x + 6, find f(2)

      What key is find function in Excel? ›

      Press Ctrl+F, and then type your search words. If an action that you use often does not have a shortcut key, you can record a macro to create one.

      What is the function to find data in Excel? ›

      The VLOOKUP or Vertical Lookup function is used when data is listed in columns. This function searches for a value in the left-most column and matches it with data in a specified column in the same row. You can use VLOOKUP to find data in a sorted or unsorted table.

      How does find () work in JS? ›

      In JavaScript, the find() method is used to search for an element in an array. It returns the first element in the array that matches the provided function. If no element matches the provided function, it returns undefined.

      Where is the find () function in C++? ›

      The find() function in C++ helps to search for an element within the specified range. This function is available in the <algorithm. h> header file.

      What does find () do in Java? ›

      Put simply, the find() method tries to find the occurrence of a regex pattern within a given string. If multiple occurrences are found in the string, then the first call to find() will jump to the first occurrence.

      How do you use the Find function on a computer? ›

      "Control+F" (or "Command+F" on a Mac) is the keyboard shortcut for the Find command.

      References

      Top Articles
      Latest Posts
      Article information

      Author: Amb. Frankie Simonis

      Last Updated:

      Views: 5777

      Rating: 4.6 / 5 (76 voted)

      Reviews: 83% of readers found this page helpful

      Author information

      Name: Amb. Frankie Simonis

      Birthday: 1998-02-19

      Address: 64841 Delmar Isle, North Wiley, OR 74073

      Phone: +17844167847676

      Job: Forward IT Agent

      Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

      Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.