23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Logical Types<br />

Logical Types<br />

The logical data type represents a logical true or false state using the<br />

numbers 1 and 0, respectively. Certain <strong>MATLAB</strong> functions and operators<br />

return logical true or false to indicate whether a certain condition was found<br />

to be true or not. For example, the statement (5 * 10) > 40 returns a logical<br />

true value.<br />

Logical data does not have to be scalar; <strong>MATLAB</strong> supports arrays of logical<br />

values as well. For example, the following statement returns a vector of<br />

logicals indicating false for the first two elements and true for the last three:<br />

[30 40 50 60 70] > 40<br />

ans =<br />

0 0 1 1 1<br />

This section covers the following topics:<br />

• “Creating a Logical Array” on page 2-33<br />

• “How Logical Arrays Are Used” on page 2-35<br />

• “Identifying Logical Arrays” on page 2-37<br />

Creating a Logical Array<br />

One way of creating an array of logicals is to just enter a true or false value<br />

for each element. The true function returns logical one; the false function<br />

returns logical zero:<br />

x = [true, true, false, true, false];<br />

Logical OperationsonanArray<br />

Youcanalsoperformsomelogicaloperationonanarraythatyieldsanarray<br />

of logicals:<br />

x = magic(4) >= 9<br />

x =<br />

1 0 0 1<br />

0 1 1 0<br />

2-33

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

Saved successfully!

Ooh no, something went wrong!