How to find a value in an array in Matlab and Octave

To search for a value in an array on Matlab and Octave, use the find(x) function


The x parameter of the find () function is a conditional expression for selecting the elements of the array.

The find () function returns the position in the index of the elements that satisfy the condition.

Note. If the search produces more than one result, the find() function returns an array with all the results. When it produces no results, the find() function returns an empty array.


Example 1

Type an array in the V variable

>> V=[ 10 15 20 25 30 ]
V =
10 15 20 25 30

Use the find function (V == 20) to find the position of the value 20 in the array

>> find(V==20)
ans = 3

The function outputs the value 3.

It means that the value 20 is in the third position in the array index

>> V(3)
ans = 20

Example 2

Search for an element with value 5 in the array

>> find(V==5)
ans = [](1x0)

In this case, the search produces no results because there is no value 5 in the array.

Example 3

Search for elements greater than 20 in the array

>> find(V>20)
ans =
4 5

The function returns an array with the values 4 and 5.

The V(4) and V(5) elements of the array have a value greater than 20

>> V(4)
ans = 25
>> V(5)
ans = 30


Report us an error or send a suggestion to improve this page

Arrays and Vectors