How to Find Index of Element in Array in MATLAB?
Last Updated :
04 Jul, 2021
In MATLAB, the arrays are used to represent the information and data. You can use indexing to access the elements of the array. In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indices and the element from the array. The find() function returns a vector containing the data.
Syntax:
- find(X) : Return a vector containing the indices of elements
- find(X,n): Return first n indices of the elements in X
- find(X,n, Direction): find n indices in X according to the Direction where Direction - 'first' or 'last'
- [row,col] = find(): It returns the row and column subscript of element in array
- [row,col,V] = find(): returns vector V containing non-zero elements
Now let's see how to find an index of any element in an array using the find() function with the help of examples.
find(x)
find(X) returns a vector containing the linear indices of each nonzero element in array X.
Example 1:
Matlab
% MATLAB code for find an index of any
% element in an array using the find()
array = [1 2 3 4 5 6]
% find() will get the index of element
% store it in the index
index = find(array==3)
Output:

Note: If the array contains duplicates then find(X) function will return all the indices of that integer.
Example 2:
Matlab
% MATLAB code for if the array contains
% duplicate elements
array = [1 2 3 4 5 6 2 4 2]
% find() will get the index of element
% store it in the index
index = find(array==2)
Output:

When the array contains duplicate values the find() function will print all the indices of that corresponding element. So if you don't want all the indices of that element you can use the find(X,n) function.
find(X,n)
Return first n indices of the elements in X.
Example:
Matlab
% MATLAB code for return first
% n indices of the elements in X
array = [1 2 3 4 5 6 2 4 2]
% find() will get the index of element
% gets the first index of 2
% store it in the index
index = find(array==2,1)
Output:

find(X,n,Direction)
You can also find the index of the elements from both directions in the array. Both directions mean from starting and from last by using find(X,n,Direction). This function find n indices in X according to the Direction. The Direction parameter accepts either 'first' or 'last'. If the direction is first it will return first n indices of that corresponding element or if the direction is last it will return the indices by traversing from the end of the array. By default, the Direction parameter is 'first'.
Example 1:
Matlab
% MATLAB code for find the index of
% the elements from both directions
% in the array
array = [1 2 3 4 5 6 2 4 2]
% find() will get the index of element
% store it in the index
index = find(array==2,2,'first')
Output:

Example 2:
Matlab
% array of integers
array = [1 2 3 4 5 6 2 4 2]
% find() will get the index of element
% store it in the index
index = find(array==2,2,'last')
Output:

[row,col] = find(x)
For finding the index of an element in a 3-Dimensional array you can use the syntax [row,col] = find(x) this will give you the row and the column in which the element is present.
Example:
Matlab
% MATLAB code for Finding an index
% of an element in a 3-D array
array = [1 2 3; 4 5 6; 7 8 9]
% find() will get the index of element
% prints the row and column of the element
[row,col] = find(array==5)
Output:

[row,col,v] = find(X)
If you want to find the indices of all the non-zero elements present in the 3-dimensional array you can use [row,col,v] = find(X) where X is our array. This will find all indices of all non-zero elements present in the array and store them into the vector v.
Example:
Matlab
% MATLAB code for find the indices of
% all the non-zero elements present in the 3-D array
x = [1 9 0; 3 -1 0; 0 0 7]
% find() will get the indices of the elements
% and the vector will store all the non-zero elements
[row,col,v] = find(x)
Output:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Software Development Life Cycle (SDLC) Software development life cycle (SDLC) is a structured process that is used to design, develop, and test good-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Waterfall Model - Software Engineering The Waterfall Model is a Traditional Software Development Methodology. It was first introduced by Winston W. Royce in 1970. It is a linear and sequential approach to software development that consists of several phases. This classical waterfall model is simple and idealistic. It is important because
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read