0% found this document useful (0 votes)
17 views4 pages

Practice Problems in Relational and Logical Operators

Uploaded by

raad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Practice Problems in Relational and Logical Operators

Uploaded by

raad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Given:

x=[1 5 2 8 9 0 1];

y=[5 2 2 6 0 0 2];

z=[2 4 1 5 0 2 6];

Find:

a) w=x>y ---- 0 1 0 1 1 0 0- compare element by element and assign 1 for


corresponding element number if condition is true and assign 0 if condition is false.
x(x>y) ---- 5 8 9----Display the value of elements of x vector which is greater than the
corresponding y vector

find(x>y)---- 2 4 5---- Display the position number of elements of x vector which is greater
than the corresponding y vector.

b) y>x----- 1 0 0 0 0 0 1
c) x==y---- 0 0 1 0 0 1 0
d) x<=y---- 1 0 1 0 0 1 1
e) x>=y---- 0 1 1 1 1 1 0
f) w=x|y----- 1 1 1 1 1 0 1

| - this symbol indicates element by element logical OR operation(addition).


If 1st elements of Vector x and vector y are non zero ,the 1st element of w is 1;
If 1st elements of Vector x and vector y are zero ,the 1st element of w is 0;
If 1st elements of Vector x zero and vector y is Non-zero ,the 1st element of w is 1;
If 1st elements of Vector x Non-zero and vector y is zero ,the 1st element of w is 1;

g) x&y -----
& - this symbol indicates element by element logical AND operation(Multiplication).
If 1st elements of Vector x and vector y are non zero ,the 1st element of w is 1;
If 1st elements of Vector x and vector y are zero ,the 1st element of w is 0;
If 1st elements of Vector x zero and vector y is Non-zero ,the 1st element of w is 0;
If 1st elements of Vector x Non-zero and vector y is zero ,the 1st element of w is 0;

w=x&y
w= 1 1 1 1 0 0 1

h) s=x&~y

s= 1 5 2 8 9 0 1& 0 0 0 0 1 0 0

s=0 0 0 0 1 0 0
i) s=x>y|x>z

s= 0 1 0 1 1 0 0| 0 1 1 1 1 0 0

s= 0 1 1 1 1 0 0

j) x>z&y>z

s= 0 1 1 1 1 0 0& 1 0 1 1 0 0 0

s= 0 0 1 1 0 0 0

2) Given:

x=1:10

x= 1 2 3 4 5 6 7 8 9 10

y= 3 1 5 6 8 2 9 4 7 0

a) z=x>3&x<8

z= 0 0 0 1 1 1 1 1 1 1&1 1 1 1 1 1 1 0 0 0

z= 0 0 0 1 1 1 1 0 0 0

b) z=x(x>5) ---- 6 7 8 9 10

c) z=y(x<=4)---- 3 1 5 6

d) z=x((x<2)|(x>=8))

=x(1 0 0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 1 1 1
=x(1 0 0 0 0 0 0 1 1 1)
= 1 8 9 10

e) z=y((x<2)|(x>=8)) ----- 3 4 7 0

f) z=x(y<0)

z=x(0 0 0 0 0 0 0 0 0 0) – all false condition


z= Empty matrix: 1-by-0
3. Given
a) set the values of x that are positive to Zero
x=[ 3 15 9 12 -1 0 -12 9 6 1]

required x= 0 0 0 0 -1 0 -12 0 0 0

Answer : It is logical array as per condition, assign 0 to the elements of x where the condition is true
x(x>0)=0
x(1 1 1 1 0 0 0 1 1 1)=0
x= 0 0 0 0 -1 0 -12 0 0 0

b)set the values that are multiples of 3 to 3


x=[ 3 15 9 12 -1 0 -12 9 6 1]

required --- x=[ 3 3 3 3 -1 3 3 3 3 1]

Answer:
x(mod(x,3)==0)=3
x( 1 1 1 1 0 1 1 1 1 0)=3
x= 3 3 3 3 -1 3 3 3 3 1

c) Multiply the values of x that are even by 5


x=[ 3 15 9 12 -1 0 -12 9 6 1]

Required x=[ 3 15 9 60 -1 0 -60 9 30 1]

Answer:
x(mod(x,2)==0)=5*x(mod(x,2)==0)

Output:
x= 3 15 9 60 -1 0 -60 9 30 1

d) Extract the value of x that are greater then 10 into a vector called y

x=[ 3 15 9 12 -1 0 -12 9 6 1]

Required y=[15 12]

Answer :

y=x(x>10)

y=x(0 1 0 1 0 0 0 0 0 0)
y= 15 12
e) set the values in x that are less than the mean to zero
Answer:
x(x<mean(x)) or x(x<sum(x)/10)
x= 0 15 9 12 0 0 0 9 6 0

You might also like