
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 Position of a Number in an Array in MATLAB
The position of a number in an array or a matrix is also known as index of that number within the array or matrix. In MATLAB, arrays are oneindexed that means the index of the first element of the array will be "1", the index of the second element will be "2", and so on.
To find the position of a number in an array, MATLAB provides various methods. Here, we will cover two commonly used methods, they are:
Using "find" function.
Using a loop mechanism.
Let us explore these methods of finding the position of a number in an array using MATLAB.
Methods 1 Find the Position of a Number in an Array using "find" Function
The "find" function is a built-in function in MATLAB which helps us to determine the position or index of a specific number in an array or matrix.
Syntax
num_position = find(A == number);
Here, A is the array and "number" is the number whose position is to determine within the array.
Let us see an example to understand the implementation of this function to determine the position of a number in an array.
Example
% MATLAB code to find the position of a number in an array % Create a sample array A = [10, 15, 20, 25, 30, 35, 15, 30, 15]; % Specify the number whose position to be determined number = 15; % Determine the position of the number in the array num_position = find(A == number); % Display the array and position of the specified number disp('Input array A:'); disp(A); disp(['Specified number: ', num2str(number)]); disp(['Position of ', num2str(number), 'in the array A is: ' num2str(num_position)]);
Output
Input array A: 10 15 20 25 30 35 15 30 15 Specified number: 15 Position of 15 in the array A is: 2 7 9
Hence, this is all about finding the position of a number in an array using the "find" function.
Let us now discuss how we can do the same by using a loop.
Methods 2 Find the Position of a Number in an Array using a Loop
In this method, we have to create a loop and iterate it through the input array to find the position of the specified number within the array.
Let us understand this method of determining the position of a number in an array with the help of an example in MATLAB.
Example
% MATLAB code to determine the position of a number in an array using a loop % Create a sample array A = [10, 15, 20, 25, 30, 35, 15, 30, 15]; % Specify the number whose position to be determined number = 15; % Create an empty array to hold the positions of the number num_position = []; % Create a loop and iterate it through the array to find the positions for i = 1:length(A) if A(i) == number num_position = [num_position, i]; end end % Display the input array, number, and its positions disp('Input array A:'); disp(A); disp(['Specified number: ', num2str(number)]); disp(['Position of ', num2str(number), 'in the array A is: ' num2str(num_position)]);
Output
Input array A: 10 15 20 25 30 35 15 30 15 Specified number: 15 Position of 15in the array A is: 2 7 9
Conclusion
This is all about finding the position of a number in an array using MATLAB. In this tutorial, we have discussed two commonly used methods of finding the position or index of a given number within an array or a matrix.