Logical functions in Matlab are very useful in such situations, when you are creating codes or functions to resolve a problem, or create codes to use in such operations. In here, we will explain the logical function in Matlab with various examples. And the uses of ‘any()’, ‘all()’ and ‘find()’ functions in Matlab will be explained with logical functions.
What Are The Logical Operators In MatLab?
You need to know about logical operators in Matlab to use logical functions effectively.
- < : Smaller than…
- <= : Smaller than or equal…
- > : Bigger than…
- >= : Bigger than or equal…
- == : Equal to…
- ~= : Not equal to…
- & : And…
- | : Or…
- ~ : Not…
These are the general logical notations in MatLab.
The Use Of ‘find()’ Command With An Example In MatLab
>> a = [1 6 4 5 6 2 1; 1 5 45 64 12 32 32; 15 52 4 63 52 14 78];
[row, column]=find(a<5 & a>2)
%first example
a(find(a>3))=5
%second example
row =
1
3
column =
3
3
a =
1 5 5 5 5 2 1
1 5 5 5 5 5 5
5 5 5 5 5 5 5
As you see above, the use of ‘find()’ command is very simple with logical operators, and also very useful in Matlab. We gave two basic examples about the use of ‘find()’ command in Matlab. First of all, we created a matrix ‘a’ which dimensions are 3×7 as you see above.
In the first example of ‘find()’ command, we want to find the adresses of elements of ‘a’ which are smaller than 5 and bigger than 2. To do it, we assigned two of variables as shown above, named ‘row’ and ‘column’. Then we entered the required logical command inside ‘find()’ command to find, what we desire. ‘a<5 & a>2’ means that, ‘elements smaller than 5 and bigger than 2’. Take a look at the results appeared in command window as ‘row’ and ‘column’.
At the second example about the use of ‘find()’ command, we want to find the elements of matrix ‘b’ which are bigger than 3, and change them with number 5.
What we did is, we wrote the required logical expression inside ‘find()’ command as ‘a>3’, and we wrote this all ‘find()’ command inside the pharantheses of matrix ‘a’. When we equalize it to number of 5, you can see the result at the end of code above. All the elements of matrix ‘a’ which are bigger than 3, changed with 5.
YOU CAN LEARN MatLab IN MECHANICAL BASE; Click And Start To Learn MatLab!
You can use ‘find()’ command in Matlab with the required combinations with logical operators to create very useful codes for your applications or projects.
The use of ‘find()’ command in Matlab with logical operators is very simple as you see. Do not forget to leave your comments and questions about the use of ‘find()’ command in Matlab effectively below.
If you want further examples about the use of ‘find()’ command in Matlab, please indicate in the comments below.
Your precious feedbacks are very important for us.
Leave a Reply