Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Number System and Arithmetic
  • Algebra
  • Set Theory
  • Probability
  • Statistics
  • Geometry
  • Calculus
  • Logarithms
  • Mensuration
  • Matrices
  • Trigonometry
  • Mathematics
Open In App
Next Article:
Void Function in MATLAB
Next article icon

Find() function in MATLAB

Last Updated : 01 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The find() function in MATLAB is used to find the indices and values of non-zero elements or the elements which satisfy a given condition. The relational expression can be used in conjunction with find to find the indices of elements that meet the given condition. It returns a vector that contains the linear indices.

Using liner index a multidimensional array can be accessed using a single subscript. MATLAB treats the array as a single column vector with each column appended to the bottom of the previous column.

Example:

For example consider the following 3x3 array A =

1 4 7

2 5 8

3 6 9

In this array all elements represents their linear index i.e. we can reference A(1,2) with A(4).

Syntax:

Below are various ways to use the function:

  • k = find(X): It returns the indices of all non zero elements.
  • k = find(X, n): It returns the first n indices of non zero elements in X
  • k = find(X, n, direction): direction can be 'first' or 'last'. If direction is first, this function will return first n indices corresponding to non zero elements and if direction is last then this function will return last n indices corresponding to non zero elements
  • [row, col] = find(): It is used to get row and column subscript for all non zero elements.
  • [row, col, v] = find(): row and column will hold subscript for all non zero elements and v is a vector which will hold all the non zero elements.

Note: k will be of same orientation as X if X is a vector and if X is a multidimensional array then k will be a column vector which will hold linear indices.

Example 1: Below code will return the indices of non-zero elements in a 1-D array.

MATLAB
% Defining array A = [1 2 3 0]  % Getting indices of non zero elements find(A) 

Output:

Example 2: Below code will return the first 2 indices of elements where the element will be greater than 3.

MATLAB
% Defining array A = [1 2 0; 3 1 4; 5 6 7]  % Getting first 2 indices find(A>3, 2) 

Output:

Example 3: Below code will return the last 2 row and column indices of elements that are greater than 3.

MATLAB
% Defining array A = [1 2 0; 3 1 4; 5 6 7]  % Getting row and column  [row, col] = find(A>3, 2, 'last') 

Output:

So, A(2, 3) and A(3, 3) are the last elements that are greater than 3. We got (2, 3) and (3, 3) as output not (3,2) and (3, 3) because MATLAB treats the array as a single column vector with each column appended to the bottom of the previous column.

Example 4: Below code will return indices of all the zero elements. This code uses the negation operator (~) in conjunction with the find function.

MATLAB
% Defining array A = [1 2 3 0]  % Getting indices of zero elements find(~A) 

Output:


Next Article
Void Function in MATLAB
author
aman neekhara
Improve
Article Tags :
  • Mathematics

Similar Reads

  • Map Function in MATLAB
    A map function basically takes the elements of the array and applies a function to each element.  The resultant output is of the same shape as the array, but the values are the result of the function. In MATLAB, there is a similar function called arrayfun(), which we can be used to achieve the same
    2 min read
  • Scripts and Functions in MATLAB
    In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following: ScriptLive ScriptFunction only fileClass fileNow only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we sh
    2 min read
  • Void Function in MATLAB
    When defining void* output in the library definition file, MATLAB specifies that the argument MLTYPE must be one of these: a typedef from the library. Use only to produce scalar data. If the library has a typedef defined for void* that uses that name, MATLAB specifies MLTYPE as the new type name in
    2 min read
  • Anonymous Functions in MATLAB
    A block of code that is organized in such a way that is reusable for the entire program. Functions are used for reducing efforts made by writing code and making the program short, and easily understandable.  There are different syntaxes for declaring a function in different programming languages. In
    5 min read
  • Defining Function Handles in MATLAB
    A MATLAB data type that represents a function is called a function handle, in other words, we say that The function handle is a typical data type in MATLAB. Function handles can therefore be modified and used in the same way as other MATLAB data types. Using function handles in arrays, structures, a
    3 min read
  • Local Functions in MATLAB
    Functions in any programming language are some blocks of code, which could be reused whenever required, by just calling the name. It reduces so much of human effort and also rewriting of the same code, and makes the entire code big. Declaring a function:Before moving forward let's see how actually t
    2 min read
  • VBA Find Function in Excel
    In an Excel sheet subset of cells represents the VBA Range which can be single cells or multiple cells. The find function will help to modify our search within its Range object. A specific value in the given range of cells is to search with the help of the Find function. Excel VBA provides different
    5 min read
  • Inline Functions in MATLAB
    Inline functions are those functions that are defined in one line, also called one-liner for the same reason. In MATLAB there are two types of inline functions, inbuilt and user-defined. Let's take a look at both of them. InBuilt inline Functions:MATLAB has many mathematical functions built-in which
    2 min read
  • Nested Functions in MATLAB
    Functions in any programming language are some blocks of code, which could be reused whenever required, by just calling the name. It reduces so much of human effort and also rewriting of the same code, and makes the entire code big.  Declaring a Function: To declare a function in MATLAB we use given
    3 min read
  • Private Functions in MATLAB
    Private functions are useful when you want to limit the scope of a function. Here we will learn how to create private functions and also use them. Private functions are primary functions that are visible only to a limited group of other functions. Generally, we make private functions, if we want to
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences