
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Index of Element in Array in MATLAB
In MATLAB, an array is a data structure used to store numeric data and information. The values stored in an array is called its element. The position of an element within an array is known as index of the element. The index of element is used to access the element or to provide a reference of the element.
In MATLAB, 1based indexing is used that means the index of the first element of an array is 1.
Consider an example array given below,
A = [20 35 74 92 60];
In this array, the index of the element '20' is 1, the index of '35' is 2, and so on.
In an array, indices of elements are crucial in various operations and computations.
This tutorial is primarily meant for learning how we can find the index of a specific element within an array. For this, MATLAB provides several builtin function and methods that we can use. Let us discuss different methods of finding the index of an element in an array using MATLAB.
(1). Find Index of an Element in an Array using "find" Function
In MATLAB, there is a built-in function "find" that can be used to determine the index of a specific element.
Syntax
I = find(array);
This syntax will return a vector containing indices of all the nonzero elements of the array.
To understand this function, let us take an example in MATLAB.
Example
% MATLAB code to find index of a specific element in an array % Create a sample array A = [1 5 7 3 4 6 8 2 6 0 9 5]; % Find the indices of elements I = find(A); % Display the input array and array of indices disp('The input array is:'); disp(A); disp('The array of indices is:'); disp(I);
Output
The input array is: 1 5 7 3 4 6 8 2 6 0 9 5 The array of indices is: 1 2 3 4 5 6 7 8 9 11 12
(2). Find Index of First "n" Element in an Array using "find" Function
The "find" can also be used to find the indices of first "n" elements of the array.
Syntax
I = find(array, n);
This syntax will return a vector containing indices of all the first "n" nonzero elements of the array.
Example
To understand this function, let us take an example in MATLAB.
% MATLAB code to find index of first "n" element in an array % Create a sample array A = [1 5 7 3 4 6 8 2 6 0 9 5]; % Find the indices of elements I = find(A, 4); % Display the input array and array of indices disp('The input array is:'); disp(A); disp('The indices of elements are:'); disp(I);
Output
The input array is: 1 5 7 3 4 6 8 2 6 0 9 5 The indices of elements are: 1 2 3 4
(3). Find Indices of "n" Element in an Array using "find" Function in a Specified Direction
The "find" can also be used to find the indices of "n" elements of the array in a specified direction. For this, the following syntax is used.
Syntax
I = find(array, n, dir);
Here, the variable "dir" can be set to "first" or "last". When "dir = first", the "find" function will return a vector containing indices of first "n" elements, and when "dir = last", the "find" function will return a vector containing indices of last "n" elements of the array.
Example
To understand this syntax, let us take an example in MATLAB.
% MATLAB code to find index of "n" elements in an array in a specified direction % Create a sample array A = [1 5 7 3 4 6 8 2 6 0 9 5]; % Find the indices of first 3 elements I_first = find(A, 3, 'first'); % Find the indices of last 3 elements I_last = find(A, 3, 'last'); % Display the input array and indices vectors disp('The input array is:'); disp(A); disp('The indices of first 3 elements are:'); disp(I_first); disp('The indices of last 3 elements are:'); disp(I_last);
Output
The input array is: 1 5 7 3 4 6 8 2 6 0 9 5 The indices of first 3 elements are: 1 2 3 The indices of last 3 elements are: 9 11 12
(4). Find Index of a Specified Element in an Array using "find" Function
In MATLAB, we can find the index of an element that satisfies a specific condition. For this, we can use the following syntax of the "find" function.
Syntax
I = find(array == element);
This syntax will return the index of the specified element of the array.
Example
To understand this function, let us take an example in MATLAB.
% MATLAB code to find index of an element meeting a condition % Create a sample array A = [1 5 7 3 4 6 8 2 6 0 9 5]; % Specify the element whose index to be found e = 6; % Find the index of element I = find(A == e); % Display the input array and index of element disp('The input array is:'); disp(A); disp('The index of specified element is:'); disp(I);
Output
The input array is: 1 5 7 3 4 6 8 2 6 0 9 5 The index of specified element is: 6 9
(5). Find Index of an Element using "ismember" Function
In MATLAB, the "ismember" function that can also be used to find the index of an element in array. For this, the following syntax is used.
Syntax
[x, I] = ismember(element, array);
Here, "x" is a variable that will be "1" if the specified element is found in the array, and the variable "I" will store the index of the element.
Example
Let us understand the use of this function with the help of an example.
% MATLAB code to find index of an element using "ismember" function % Create a sample array A = [1 5 7 3 4 6 8 2 6 0 9 5]; % Specify the element whose index to be found e = 6; % Find the index of the element [x, I] = ismember(e, A); % Show whether the element is found or not if x == 1 disp('The specified element is found in the array.'); else disp('The specified element is not found in the array.'); end % Display the input array and index of element disp('The input array is:'); disp(A); disp('The index of specified element is:'); disp(I);
Output
The specified element is found in the array. The input array is: 1 5 7 3 4 6 8 2 6 0 9 5 The index of specified element is: 9
Conclusion
In conclusion, the index of an element in an array is the position of the element within the array. In this tutorial, I have explained different methods of finding indices of elements in an array with the help of examples.