Updated March 15, 2023
Introduction to Matlab find value in array
The following article provides an outline for Matlab find value in array. In matlab a function is used to find indices values and values of nonzero elements in the array known as “find values in array.” The find values in the array will help find the elements or numbers present in the given array or not.
Syntax:
A = find(Z)
A = find(Z,n)
How to find value in an array?
Matlab find values in array used for find indices and values of nonzero elements in the given array. To find values of nonzero elements in array, we need to take all elements in array and use proper syntax.
The steps for find values of nonzero value using find values in array:
- Step 1: We need to collect all inputs in one set or in an array.
- Step 2: Then, we use a find value in array with proper syntax to find the nonzero element values.
Examples of Matlab find value in array
Given below are the examples of Matlab find value in array:
Example #1
Let us see an example related to matlab find values in array, as we know find values in array is used for find indices and values of nonzero elements in the given array. So in this example, we take a number in the range of 1 to 30 with the difference of 2, and these elements take into a variable ‘F’ the numbers are 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27 and 29. Now we want to find a specific element in the array ‘F’ for that; we use a == operator. Now we use find values in array to find a specific element in the array ‘F.’ Now, we find that 13 and 12 are present in the array or not. For that we can use find values in array as “D1 = find(F==13)” And “D2 = find(F==12)”. This line will find whether a 13 number is present in the given array or not, and if the number is present in the array, the function returns the position of that number into the array. And the number is not present, then it displays a message as “Empty matrix.”
Code:
clc;
clear all;
close all;
F = 1:2:30
D1 = find(F==13)
D2 = find(F==12)
Output:
After executing the above Matlab code 1st, we created array F. We found the 13 number at 7th place in the array F. However, the number 12 is not present into the array; hence matlab find values in array function returns empty.
Example #2
Let see one more example of matlab find values in array function. In this example, we create a matrix, and then we see how matlab finds values in array works. So first, we started with creating a 2–by–2 matrix that contains random integer numbers among 1 to 4. Next, we used the magic function to create a 2–by–2 matrix. Then we used matlab to find values in the array function. For example, Z= magic(2) returns a 2–by–2 matrix with random integers between 1 and 4. After that, we used the “A = find(Z)” syntax, which returns the values of nonzero elements in the array.
Code:
clc;
clear all;
close all;
Z= magic(2)
A = find(Z)
Output:
After executing the above code magic function, created a 2–by–2 matrix containing random integer numbers of 1 to 4. And after that, matlab find values in array function returns the all elements of matrix Z into the variable A.
Example #3
Let us see another example; as we know, find values in array are used to find indices and nonzero elements in the given array. So in this example, we will see that finding values in array finds the irrational or decimal numbers efficiently. For this, we take decimal numbers in the range of 0 to 3 with the difference of 0.3, and these elements take into a variable ‘G’ the numbers are 0, 0.3000, 0.6000, 0.9000, 1.2000, 1.5000 1.8000, 2.1000, 2.4000, 2.7000 and 3.0000. Now we want to find a 1.2 decimal number in the array ‘G’ for that; we use a == operator. So now we use find values in array to find 1.2 decimal numbers in the array ‘G.’ R1 = find(G== 1.2) returns the position of the decimal number 1.2 in array G. The position is stored into the variable R1.
Code:
clc;
clear all;
close all;
G = 0:0.3:3
R1 = find(G== 1.2)
Output:
After executing the above matlab code, we created an array G of decimal numbers. We found the 1.2 number at the 5th position in the array G.
Conclusion
In this article, we saw the concept of Matlab find values in array. Basically, matlab finds values in array used for indicating values of an element into the array. Matlab find values in array plays an important role for finding a position of elements in the array. If there is no element in to the array, it returns empty.
Recommended Articles
This is a guide to Matlab find value in array. Here we discuss the introduction, how to find value in array? And examples respectively. You may also have a look at the following articles to learn more –