lettura simple

How to find a value in an array in Octave

In this guide I'll explain how to search and find data in an array using the find() function of Octave


The find() function has three parameters:

  • X
    The first parameter X is an array where to search. It is a required parameter.
  • N
    It's the maximum number of items you want to get from the search. The search stops after N results.
  • D
    It is the direction of search. You can enter "first" to search from the first to the last element or "last" to search from the last to the first element of the index. By default it is "first".

This function extracts the indexes of the elements that match the search from the array.

Note. The find() function returns the indexes of the arrays in ascending order. Not in order of extraction.

I'll give you a practical example

Create an array and assign it to the variable V

>> V = [ 1 0 2 0 4 0 4 ]
V =
1 0 2 0 4 0 4

There are seven elements in the array

Don't forget that the first element of an array has zero index in Octave

index of the elements of the array

Type find(V) to find the non-zero elements

>> find(V)
ans =
1 3 5 7

The find() function displays the index position of the elements of the array with non-zero values, i.e. V[1], V[3], V[5] and V[7].

the result of search

The results are the positions of the elements in the index of the array. They are not the values of the elements.

Note. The find() function returns the same results even when the array is arranged in a column.
the function find()

Type find(V,1) to get only one result from the search.

>> find(V,1)
ans = 1

The function returns the position of the first non-zero element in the array, i.e. V[1]

the result of the search

Now, type find(V,1,"last") to search from right to left.

>> find(V,1,"last")
ans = 7

The result is the element V[7] because it is the first with non-zero value V[7]=4 starting from the right.

the result of the search

If you want to find elements equal to zero, type a negation before the array name.

Type find(~V)

>> find(~V)
ans =
2 4 6

The search extracts the positions in the index of the elements with value equal to zero.

The results are V[2], V[4] and V[6].

the result of the search

Type find(V>2) to find only array elements with values greater than two

>> find(V>2)
ans =
5 7

The function finds the elements V[5] and V[7] of the array

the result of the search

When the search produces no results, the find() function returns an empty set.

For example, if you type find(V>5) the result is an empty array.

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

Type find(V<=3 & V>=1) to search for array elements with value between 1 and 3

>> find(V<=3 & V>=1)
ans =
1 3

The search extracts the elements V[1] and V[3] of the array

the result of the search

How to do a search in a multidimensional array?

When the array is multidimensional (e.g. matrix) the find function searches for the data column by column.

For example, create a matrix with three rows and three columns.

>> M=[1 0 1; 0 1 0; 1 0 1]
M =
1 0 1
0 1 0
1 0 1

The array is a two-dimensional array

$$ \begin{pmatrix} 1 & 0 & 1 \\ 0 & 1 & 0 \\ 1 & 0 & 1 \end{pmatrix} $$

Type find(M) to search for elements with non-zero value

>> find(M)
ans =

The function returns the positions of the elements in the index as if the array were one dimension, by iterating the elements in column.

For example, indexes 1 and 3 are the first two non-zero elements found on the first column.

the first two elements

Index 5 is the nonzero element on the second column.

It is the fifth element analyzed by the research.

the fifth element of the series

Indices 7 and 9 are the non-zero elements on the third column.

They are respectively the seventh and ninth element in the sequence of elements analyzed by the search.

the elements in position 7 and 9

If you want to get as a result the coordinates (row;column) of the elements in the matrix, you have to use another command.

Type [row,col,v]=find(M)

>> [row,col]=find(M)
row =

col =

v =

In this case the function extracts three arrays:

  • the first array (row) contains the row numbers
  • the second array (col) contains the column numbers
  • the third array (v) contains the values of the elements

For example, to display the coordinates of the first search result, type row(1),col(1)

>> row(1),col(1)
ans = 1
ans = 1

The first non-zero element of the matrix is found at the coordinates (1,1) ie on the first row and first column of the matrix.

the first element

To view the coordinates of the second result, type row(2),col(2)

>> row(2),col(2)
ans = 3
ans = 1

The second element with a non-zero value is found at coordinates (3,1) ie on the third row and first column of the matrix.

the second element

This way you can get the exact position of the elements on the array.

Report a mistake or post a question