Logic operations are used for analysis of situations like:
- 2 is bigger than 3? - false
- the vector [1 2 3 4] is larger than [1 3 5]? - true
- the word 'horse' is smaller than 'dog'? - false
The logic operations combine two or more situations, for example:
I'm 22, my father's 52 and my sister's 19.
The question: who is the older?
The solution:
- if I'm older than my father and my sister then I'm the older.
- if my father's older than me and my sister then he's the older.
- if my sister's older than my father and me then she's the older.
The logic operators are used with logic variables. Logic variables are like numeric variables but the values are true or false.
The basic logic operators are: {not, or, and}
The not is a unary operator and the others are binary operators.
So, follow the possible logic situations for each operation:
NOT
- not(true) is false
- not(false) is true
AND
- false and false is false
- false and true is false
- true and false is false
- true and true is true
OR
- false or false is false
- false or true is true
- true or false is true
- true or true is true
A mental train.
If I want a racket and a ball but I have just a racket I'm not satisfied. But, If I want a ball of basketball or a ball of soccer, so I need one ball or both.
The Scilab has that three operators.
The operator '~' is the not.
The operator '|' is the or.
The operator '&' is the and.
The operations or and and can be applied on vectors with the functions or(.) and and(.).
Let's play now.
-->x = 10;
-->y = 15;
-->z = x + y
z =
25.
-->(x > y) & (z > x) // x is smaller than y then the first operation is false
ans =
F
-->(x > y) | (z > x) // z is bigger than x then the second operation is true
ans =
T
-->~((x > y) & (z > x)) // the result is the inverse of the first result
ans =
T
-->~((x > y) | (z > x)) // the result is the inverse of the second result
ans =
F
The results are F (false) or T (true) because logic operations give logic results.
If you want use the variables with logic values:
-->r1 = %F
r1 =
F
-->r2 = %T
r2 =
T
-->and([r1 r2])
ans =
F
-->or([r1 r2])
ans =
T
For finish the post, if you use a numeric variable with logic operators, thus the Scilab interprets the 0 (zero) as false and different of 0 (zero) as true.
-->x = 0;
-->y = 2;
-->z = -5;
-->x | y | z
ans =
T
-->x & y & z
ans =
F
-->x & z
ans =
F
-->x & y
ans =
F
-->y & z
ans =
T
5 comments:
Man this tutorial of yours is really useful, i'm a master student too and this is really gonna save me.
Thank you!
Thanks and Keep up the great work!
nice tutorial...
Poda punda...........
Grt Tutorial dude...
Post a Comment