Updated March 10, 2023
Introduction to Matlab find Index
Matlab finds the index function used to remit a vector containing the linear indices of each nonzero element in the array. For Matlab find the index “Find” statement is used. For example, D = find(Y) remits a vector containing the linear indices of each nonzero element in array Y. If Y is a vector, then find returns a vector with the same orientation as Y. If Y is a multidimensional array, then find returns a column vector of the linear indices of the result. If Y contains no nonzero elements or is empty, then find returns an empty array.
Syntax
- D = find(Y)
- D = find(Y,n)
- D = find(Y, n, direction )
- [row, col] = find (___)
- [row, col, v] = find (___)
How to Do Matlab Fscanf?
Find the function used to find indices and values of nonzero value. For finding indices and values of nonzero elements, we need to take all elements into a variable and use proper syntax.
The steps to find indices and values of nonzero value using find the statement:-
Step 1: We need to take all elements into a variable
Step 2: Then, we use a find statement with proper syntax to find indices and values of the nonzero element.
Examples of Matlab find Index
Given below are the examples of Matlab find Index:
Example #1
Let us see an example related to find function, find function used to find indices and values of nonzero value. So in this example, we take a 3-by-3 matrix, and this matrix takes into a variable ‘Y’. So index 1 starts with row 1 and column one, and the next index value is 2 corresponds to row 2 and column one, then the same sequence will continue till the last index value; in our example, the last index value is 9 because we take 3-by-3 matrix. So now we use a find function; we take to find(Y); this command displays the indices of nonzero values like in our examples non zero values are 1, 2, 5, 7 and 4 the find displays the indices values of these numbers, and then we take a complement of 1st command that is found (~Y), this command is used to display the indices value of zero values present in the matrix so that indices are stored in d2 variable.
Code:
clc;
close all;
Y = [1 0 2; 0 5 7; 0 0 4]
D = find(Y)
D2 = find(~Y)
Output:
As we saw the result, the Y variable shows the 3 by 3 matrix, then the D stores the indices values of nonzero values present at the Y matrix and the D2 stores the value of indices of a zero present in the matrix. The result of D and D2 shows in n*1 format, where n is the number of rows.
Example #2
In this example, we take a 5 by 5 matrix using a magic function; magic creates a matrix; if we take magic(n), it creates a by n matrix; the matrix number is 1 to n^2 with equal rows and columns. So in our example, we take magic(5) which means it creates a 5 by 5 matrix; the numbers are 1 to 25 because 5^2. This matrix we assign to the Y variable. Then now, we use a find function, find(Y<15,10); it is used to find the first 10 elements that are less than 15 in a 5 by 5 magic square matrix, and the indices values of that 10 numbers are stored in D variable and display it.
Code:
clc;
clear all;
close all;
Y = magic(5)
D = find(Y<15,10)
Output:
Example #3
Let us see another example related to find function, as we know to find function used to find indices and values of nonzero value. So in this example, we take a number in the range of 10 to 30 with the difference of 3, and these elements take into a variable ‘Y’ the numbers are 13, 16, 19, 22, and 25. Now we find a specific integer value for that we use a == operator, so we write command as find(Y==22) then it displays the index value where 22 is present. If that number is not present in that row, then it returns an empty.
Code:
clc;
clear all;
close all;
Y = 10:3:30
K1 = find(Y==22)
K2 = find(Y==20)
Output:
Example #4
Let us see an example for find function, as we know to find function used to find indices and values of nonzero value. So in this example, we take a number in the range of 1 to 3 with the difference of 0.3, and these elements take into a variable ‘Y’ the numbers are 1.3, 1.6, 1.9, 2.2, 2.5, and 28. Now we find a specific value for that we use a == operator, so we write command as find(Y==1.6) then it displays the index value where 1.6 is present. If that number is not present in that row, then it returns an empty.
Code:
clc;
clear all;
close all;
Y = 1:0.3:3
K1 = find(Y== 1.6)
Output:
Conclusion
In this article, we saw the concept of Matlab find the index. Basically, Matlab finds index is used for indicating values of the nonzero element. Then saw syntax related to Matlab find index statements and how it’s used in Matlab code. Also, we saw some examples related to Matlab’s find index statement.
Recommended Articles
This is a guide to Matlab find Index. Here we discuss How to Do Matlab Fscanf, and Examples of Matlab find Index along with the codes and outputs. You may also have a look at the following articles to learn more –