How to Find Index of Element in Array in MATLAB?
Last Updated :
23 Jul, 2025
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
How to find sum of elements of an array in MATLAB? This article will discuss the "Finding sum of elements of an array" in MATLAB that can be done using multiple approaches which are illustrated below. Â Using sum(A)Â This is used to return the sum of the elements of the array along the first array dimension whose size does not equal 1. It returns a ro
4 min read
Find the Index of an Array Element in Java In Java, arrays are one of the most commonly used data structures for storing a collection of data. Here, we will find the position or you can index of a specific element in given array.Example: Input: a[] = { 5, 4, 6, 1, 3, 2, 7, 8, 9 }, element = 7 Output: 6 1. Using a Simple LoopOne of the simple
5 min read
How to Find Indices and Values of Nonzero Elements in MATLAB? MATLAB provides functionality that finds the indices and values of all non-zero elements in a vector or multidimensional array; the find() function. The find function with required parameters gives back a vector containing indices of non-zero elements in the passed array/vector. Syntax: vec = [] %so
3 min read
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
How to find the Index of value in Numpy Array ? In this article, we are going to find the index of the elements present in a Numpy array.Using where() Methodwhere() method is used to specify the index of a particular element specified in the condition.Syntax: numpy.where(condition[, x, y])Example 1: Get index positions of a given valueHere, we fi
5 min read
MATLAB Find Exact String in Cell Array Cell arrays in MATLAB store data of various data types as a cell. These cells could contain data of different types but belong to the same array. Now, this article is focused on finding an exact string in a cell array in MATLAB. This can be done easily by using a combination of two MATLAB functions,
2 min read