02.03.2016 Views

MATLAB by rudra pratap

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.2 Matrix and Array Operations<br />

75<br />

These operators work in a similar way as the relational operators and produce<br />

vectors or matrices of the same size as the operands, with 1 where the condition is<br />

true and 0 where false.<br />

Examples: For two vectors x = [0 5 3 7] and y = [0 2 8 7] ,<br />

m = (x>y)& (x>4)<br />

n x \ y<br />

m = -cx\y)<br />

p = xor(x,y)<br />

results in m =[0 1 0 OJ , because the condition is true only for x2 ,<br />

results in n =[0 1 1 1], because either Xi or Yi is nonzero for i = (2 3 4],<br />

results in m =[1 0 0 OJ, which is the logical complement of x I y, and<br />

result:; in p =[0 0 0 0] , because there is no such index i<br />

for which x, or yi , but not both, is nonzero.<br />

Because the output of the logical operations is a 0-1 vector or a 0-1 matrix, the<br />

output can be used as the index of a matrix to extract appropriate elements. For<br />

example, to see those elements of x that satisfy both the conditions (x > y) and<br />

(x > 4), type x( (x>y)& (x>4) ).<br />

In addition to these logical operators, there are many useful built-in logical<br />

functions, such as:<br />

all<br />

any<br />

exist<br />

is empty<br />

isinf<br />

isfinitQ<br />

isnan 3<br />

find<br />

true ( = 1) if all elements of a vector are true,<br />

Example: all (xlOO) returns the row and column indices<br />

i and j of A, in vectors r and c, for which Aii > 100.<br />

To complete this list of logical functions, we just mention isreal , is sparse ,<br />

isstr, and ischar.<br />

3.2.4 Elementary math functions<br />

All of the following built-in math functions take matrix inputs and perform array<br />

operations (element <strong>by</strong> element) on them. Thus, they produce an output matrix of<br />

the same size as the input matrix.<br />

Fo r on-line help<br />

type:<br />

help elfun<br />

Trigonometric functions<br />

sin, sind<br />

asin, asind<br />

cos, cosd<br />

acos , acosd<br />

tan, tand<br />

sine,<br />

inverse sine,<br />

cosine,<br />

inverse cosine,<br />

tangent ,<br />

sinh<br />

a sinh<br />

cosh<br />

a cosh<br />

tanh<br />

hyperbolic :sine,<br />

inverse hyperbolic sine,<br />

cyperbolic cosine,<br />

inverse hyperbolic cosine,<br />

hyperbolic tangent,<br />

3The function isnan is the only way to check for NaNs in a matrix because any uveralion with a<br />

NaN produces a NaN and two NaNs are not equal to each other. Thus, find(A==nan) cannot find<br />

the indices of NaNs in matrix A. Of course, you could use some clever trick, such as find (A*0-=0) .

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!