How to find data in a matrix in Matlab and Octave
Use the find() function to find data in an array on Matlab and Octave
find(condizione);
The argument is a condition to select the elements of the array.
The find () function returns the list of elements that satisfy the selection conditions.
Examples
Example 1
Create a 3x3 matrix
>> M=[1 2 3; 4 5 6; 7 8 9]
M =
1 2 3
4 5 6
7 8 9
Type [row, col]=find(M==5) to find the value 5 in the array
>> [row,col]=find(M==5)
This command finds the value 5 at the coordinates row = 2 and col = 2
row = 2
col = 2
The value 5 is located at the coordinates (2; 2) that is on the second row and second column of the matrix.
Example 2
Type [row,col]=find(M<3) to find values less than 3 in the matrix
>> [row,col]=find(M<3)
In this case the variables row and col are two arrays because the selection finds more than one value that satisfies the condition.
row = [1 1]
col = [1 2]
The first element is located at the coordinates row(1)=1 e col(1)=1 that is to the coordinates (1; 1) of the matrix.
The second element is located at the coordinates row(2)=1 e col(2)=2 that is to the coordinates (1; 2) of the matrix.
Example 3
Type [row,col]=find(M>10) to find values greater than 10 in the matrix
>> [row,col]=find(M>10)
The matrix has no values greater than 10.
The row and col arrays are empty.
row = [](0x1)
col = [](0x1)