How to Find Index of Value in Array in Matlab

Ammar Ali Feb 12, 2024
  1. Find Index of Value in Array in MATLAB Using the find() Function
  2. Find Index of Value in Array in MATLAB Using Loops
  3. Find Index of Value in Array in MATLAB Using the ismember() Function
  4. Conclusion
How to Find Index of Value in Array in Matlab

In MATLAB, the ability to locate the index of a specific value within an array is a fundamental skill. Whether you’re a beginner or an experienced MATLAB user, there are various techniques at your disposal to efficiently tackle this common programming task.

In this article, we’ll explore different methods to find the index of a value in an array, ranging from basic to advanced approaches.

Find Index of Value in Array in MATLAB Using the find() Function

MATLAB provides a handy function called find() to simplify locating the index of a specific value within an array.

The find() function in MATLAB is designed to locate nonzero elements of an array. It returns the indices of the elements that satisfy a given condition.

While it’s typically used to find nonzero elements, it can also be employed to find the index of a specific value by comparing array elements with the desired value.

Syntax of the find() function for our purpose:

indices = find(array == value);

Here, array is the input array, value is the specific value you want to find, and indices is the resulting array containing the indices where the specified value occurs.

Example 1: Finding the Index of a Single Element

Let’s begin with a straightforward scenario where we want to find the index of a single element in an array.

% Example 1: Finding the Index of a Single Element
array = [2, 3, 1, 2];
indices = find(array == 2);
indices

In this example, we have an array [2, 3, 1, 2], and we use the find() function to locate the indices where the value is equal to 2. The result is stored in the variable indices.

Output:

indices =

   1   4

The output shows that the element 2 is present at indices 1 and 4.

Example 2: Finding Indices With a Condition

Now, let’s explore a scenario where we find indices based on a specific condition, such as elements greater than a certain value.

% Example 2: Finding Indices with a Condition
array = [2, 3, 1, 2];
indices = find(array > 1);
indices

Here, we modify the condition inside the find() function to identify indices where elements are greater than 1.

Output:

indices =

   1   2   4

The output indicates that elements at indices 1, 2, and 4 satisfy this condition.

Example 3: Working With Matrices

The find() function is not limited to arrays; it works seamlessly with matrices. Let’s explore finding indices in a matrix.

% Example 3: Finding Indices in a Matrix
matrix = [2 3; 2 1];
[row, col] = find(matrix == 2);
row
col

In this example, we have a matrix with two rows and two columns. We use the find() function to locate the row and column indices where the value is equal to 2.

Output:

row =

   1
   2

col =

   1
   1

The output reveals that the value 2 is present at (1,1) and (2,1).

Example 4: Modifying Values Using Found Indices

Beyond just finding indices, we can use them to modify values within the array or matrix.

% Example 4: Modifying Values Using Found Indices
matrix = [2 3; 2 1];
indices = find(matrix == 2);
matrix(indices) = 5;
matrix

Here, we find the indices of the element 2 and replace those values with 5.

Output:

matrix =

   5   3
   5   1

The output showcases the updated matrix.

The find() function in MATLAB provides an efficient way to locate the indices of specific values within arrays or matrices. Whether dealing with single elements or matrices, the flexibility of this function makes it a valuable tool for various scenarios.

Find Index of Value in Array in MATLAB Using Loops

While MATLAB provides convenient built-in functions like find() to locate the index of a value in an array, it’s also valuable to explore alternative approaches for educational purposes.

To find the index of a value in an array using a loop, we can iterate through each element of the array and compare it with the desired value. Once a match is found, we can store the index and exit the loop.

Example 1: Finding the Index of a Single Element

Let’s begin with a simple scenario where we find the index of a single element in an array using a loop.

% Example 1: Finding the Index of a Single Element Using a Loop
array = [2, 3, 1, 2];
target_value = 2;
custom_indices = [];
for i = 1:length(array)
    if(array(i) == target_value)
        custom_indices = [custom_indices i];
    end
end
custom_indices

In this example, we have an array [2, 3, 1, 2], and we use a loop to traverse its elements. The if statement checks if the current element is equal to the target value (2), and if so, the index is appended to the custom_indices array.

Output:

custom_indices =

   1   4

Here, the output indicates that the element 2 is present at indices 1 and 4.

Example 2: Handling Different Conditions

Next, let’s explore a scenario where we find indices based on a specific condition, such as elements greater than a certain value.

% Example 2: Finding Indices with a Condition Using a Loop
array = [2, 3, 1, 2];
condition_value = 1;
custom_indices_condition = [];
for i = 1:length(array)
    if(array(i) > condition_value)
        custom_indices_condition = [custom_indices_condition i];
    end
end
custom_indices_condition

In this case, the loop checks if each element is greater than 1 and if true, the index is recorded.

Output:

custom_indices_condition =

   1   2   4

The output shows that elements at indices 1, 2, and 4 satisfy the condition specified.

Example 3: Handling Matrices With Nested Loops

Loop-based indexing can also be extended to matrices, where nested loops are employed to traverse both rows and columns.

% Example 3: Finding Indices in a Matrix Using Nested Loops
matrix = [2 3; 2 1];
target_value_matrix = 2;
[row_matrix, col_matrix] = deal([]);
for row = 1:size(matrix, 1)
    for col = 1:size(matrix, 2)
        if(matrix(row, col) == target_value_matrix)
            row_matrix = [row_matrix row];
            col_matrix = [col_matrix col];
        end
    end
end
row_matrix
col_matrix

In this example, two nested loops traverse the rows and columns of the matrix. The if statement checks if the current matrix element is equal to the target value (2), and if so, the row and column indices are recorded.

Output:

row_matrix =

   1   2

col_matrix =

   1   1

As we can see, the output reveals that the value 2 is present at (1,1) and (2,1). Loop-based indexing provides an alternative approach to finding indices in MATLAB, particularly useful in scenarios where custom conditions or intricate matrix structures are involved.

Find Index of Value in Array in MATLAB Using the ismember() Function

Another powerful tool for finding indices of a specific value in an array is the ismember() function. This function is particularly useful when dealing with scenarios where we want to check the membership of values in an array.

The ismember() function in MATLAB is designed to determine if elements of one array are members of another array. While its primary use is for membership testing, it can be harnessed to find the index of a value in an array by exploiting the logical indexing feature.

The basic syntax of the ismember() function is as follows:

tf = ismember(A, B);

Here, A is the array in which we want to find the indices, and B is an array or a set of values whose membership we are checking. The function returns a logical array tf with the same size as A, where tf(i) is true if A(i) is a member of B, and false otherwise.

Example 1: Finding the Index of a Single Element

Let’s begin with a straightforward scenario where we use the ismember() function to find the index of a single element in an array.

% Example 1: Finding the Index of a Single Element Using ismember()
array = [2, 3, 1, 2];
target_value = 2;
tf = ismember(array, target_value);
indices_ismember = find(tf);
indices_ismember

In this example, we have an array [2, 3, 1, 2], and we use ismember() to check if each element is a member of the target value (2). The logical array tf is then passed to the find() function to obtain the indices where the condition is true.

Output:

indices_ismember =

   1   4

The output indicates that the element 2 is present at indices 1 and 4.

Example 2: Handling Different Conditions

The ismember() function is versatile and allows us to handle various conditions easily, such as finding indices based on elements greater than a certain value.

% Example 2: Finding Indices with a Condition Using ismember()
array = [2, 3, 1, 2];
condition_values = [1, 3];
tf_condition = ismember(array, condition_values);
indices_condition_ismember = find(tf_condition);
indices_condition_ismember

Here, the ismember() function checks if each element in the array is a member of the specified condition values (1 and 3). The resulting logical array is then passed to find() to obtain the indices where the condition is true.

Output:

indices_condition_ismember =

   2   3

The output shows that elements at indices 1 and 3 satisfy the condition that was specified in the code.

Example 3: Handling Matrices

The ismember() function seamlessly extends its functionality to matrices. Let’s explore finding indices in a matrix.

% Example 3: Finding Indices in a Matrix Using ismember()
matrix = [2 3; 2 1];
target_value_matrix = [2, 1];
tf_matrix = ismember(matrix, target_value_matrix);
[row_matrix_ismember, col_matrix_ismember] = find(tf_matrix);
row_matrix_ismember
col_matrix_ismember

In this example, we have a matrix with two rows and two columns. The ismember() function checks membership for each element in the matrix with the target values (2 and 1).

The resulting logical array is then passed to find() to obtain the row and column indices where the condition is true.

Output:

row_matrix_ismember =

   1
   2
   2

col_matrix_ismember =

   1
   1
   2

The output reveals that the values 2 and 1 are present at (1,1), (2,1), and (2,2).

The ismember() function offers a concise solution for finding indices of specific values in MATLAB arrays or matrices. Its ability to handle different conditions and seamlessly work with matrices makes it a valuable tool in various scenarios.

Conclusion

MATLAB provides several effective methods for finding the index of a value in an array, each catering to different needs and scenarios.

The find() function stands out as a versatile tool, allowing users to locate indices based on specific conditions efficiently. Whether searching for a single element or handling matrices, the find() function provides a straightforward and powerful solution.

For those who prefer a custom approach, looping through the array with an if statement offers a flexible alternative, allowing users to implement customized conditions and gain a deeper understanding of the indexing process.

Moreover, the ismember() function provides a concise and readable solution, particularly beneficial when dealing with membership conditions and matrices. Its simplicity makes it a valuable choice for scenarios where a direct check for membership is the primary concern.

Ultimately, the choice of method depends on the specific requirements of the task at hand. Whether it’s the flexibility of custom loops, the versatility of the find() function, or the simplicity of ismember(), MATLAB provides a rich set of tools for efficiently finding the index of a value in an array.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Array