17
Logical Vectors and Strings

Logical vectors

Embed Size (px)

Citation preview

Page 1: Logical vectors

Logical Vectors andStrings

Page 2: Logical vectors

• TYPE as it is in Command Line :

r=1; Chk result

r<=.5

( Result is 0)

and then at last

r>=.5

( Result is 1)

Now Enter the Following statement :

r = 1:5;

r<=3

U get 11100 as statement is true for first 3 elements

Page 3: Logical vectors

• Vectors are compared element by element and the resulting vector is called as Logical Vector. It is one of the most power concepts in MATLAB.

• For eg try this :

a=1:5;

b=[0 2 3 5 6];

a==b % no semicolon (Result is :

0 1 1 0 0)

Page 4: Logical vectors

• Applications :

Discontinuous Graphs :

Type these Statements :

x=0:pi/20:3*pi;

y=sin(x);

y=y .* (y>0);

plot(x,y)

Page 5: Logical vectors

• What was the Funda ???

y>0 returns a logical vector with ones where sin(x) is positive as y>0 means it is checked element by element if it is >0 ; and if it is not then it returns 0.

After this we multiply by y to get the graph

Page 6: Logical vectors

• Avoid division by 0

As one of the element of x is exact zero.

Page 7: Logical vectors

• Use Logical Vector to replace zero with eps.

What is eps?

• This MATLAB function returns the difference between 1.0 and the next largest number which can be represented in MATLAB, i.e. approximately 2.2e-16.

Page 8: Logical vectors

• So you type and get :

Page 9: Logical vectors
Page 10: Logical vectors

• Logical Functions :

x is a vector :

Try these statements :

x=1:5

any(x)

any(x) : returns scalar 1 in ans if any element of x is nonzero.

Page 11: Logical vectors

• Try for the o/p of

x = [ 0 0]

any(x)

all(x)

Returns scalar 1 if all elements of vector x r non zero!

x=1:5;

all(x)

Try to get info abt all logic functions

Page 12: Logical vectors
Page 13: Logical vectors

• Strings :

Page 14: Logical vectors

• For Basic i/p o/p of string we have :

name = input( 'Enter your surname: ', 's' );

Chk the workspace

Now type

disp(name)

Page 15: Logical vectors
Page 16: Logical vectors
Page 17: Logical vectors