Matlab Operations
Matlab Operations
Operation Description
== Equal
~= Not equal
< Less than
> Greater than
<= Less than or equal
>= Greater than or equal
Example:
t =
0 0 0 0 1 1 1 1 1
23
Experiment No. (4) Relational and Logical Operations
>> t= (a==b) %finds elements of (a) that are equal to those in (b).
t =
0 0 0 0 0 0 0 0 0
Operation Description
& Logical AND and(a,b)
| Logical OR or(a,b)
~ Logical NOT
xor (a,b) Logical EXCLUSIVE OR
Example:
>> a = [0 4 0 -3 -5 2];
>> b = ~a
b =
1 0 1 0 0 0
>> c=a&b
c =
0 0 0 0 0 0
solution
a)
>> x>2
ans =
0 0 1
0 1 0
24
Experiment No. (4) Relational and Logical Operations
b)
>> t=~(~x);
>> sum(sum(t))
ans =
Operation Description
bitand (A, B) Bitwise AND
bitor (A, B) Bitwise OR
bitset (A, BIT) sets bit position BIT in A to 1
bitget (A, BIT) returns the value of the bit at position BIT in A
xor (A, B) Bitwise EXCLUSIVE OR
>> bitget(A,3)
ans = 1 where A= 1 0 1 0 0 0 0 0
>> bitget(A,(1:8))
ans =
1 0 1 0 0 0 0 0
>> bitand(A,B)
ans =
4
>> and(A,B)
ans =
1
25
Experiment No. (4) Relational and Logical Operations
Function Description
any(x) True if any element of a vector is a nonzero number or is logical 1 (TRUE)
all(x) True if all elements of a vector are nonzero.
find(x) Find indices of nonzero elements
isnan(x) True for Not-a-Number
isinf(x) True for infinite elements.
isempty(x) True for empty array.
>> any(A)
ans = 1
>> all(A)
ans = 0
>> find(A)
ans = 1 2 3 5
26
Experiment No. (4) Relational and Logical Operations
Exercises
a) v = (x and y) or z
b) w = not (x or y) and z
c) u = (x and not (y)) or (not (x) and y)
2- Write a program for three bits parity generator using even-parity bit.
3- Write a program to convert a three bits binary number into its equivalent gray code.
a) v = x > y
b) w = z >= y
c) u = ~z & y
d) t = x & y < z
27