MATLAB Programming/Boolean and Rational

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

MATLAB Programming

Chapter 1: A Tutorial Introduction

Chapter 2: Basic MATLAB Concepts

Saving and loading a MAT-file
MATLAB's Command Prompt
Basic Reading and Writing Data from a File

Chapter 3: Data Storage and Manipulation

Data Types and Operations on Point Values

Boolean and Rational
Strings
Portable Functions
Complex Numbers

Arrays and Matrices

What is an array?
Introduction to array operations
Vectors and basic vector operations
Struct Arrays
Cell Arrays
Sparse Matrices

Chapter 4: Graphics

Basic Graphics Commands
Annotating Plots

Chapter 5: M File Programming

Scripts
Comments
The Input Function
Control Flow
Loops and Branches
Error Messages
MATLAB Caveats
Debugging M Files

Chapter 6: Mathematical Manipulations

Linear Algebra

Simple matrix manipulation
More complicated matrix operations

Differential Equations

Ordinary Differential Equations
Partial Differential Equations

Chapter 7: More advanced I/O

Writing and Reading to a Serial Port
Writing to a USB Port

Chapter 8: Examples

Filtering
Controls
Phase Vocoder and Encoder

Chapter 9: Object-Oriented Programming

Struct arrays
MATLAB Classes

Chapter 10: An alternative to MATLAB: Octave

What is Octave ?
Octave/MATLAB differences

Chapter 11: Toolboxes

Symbolic Math Toolbox
GUIDE
Simulink
Psych Toolbox

edit this box


Contents

[edit] Introduction

A large number of MATLAB's functions are operations on two types of numbers: rational numbers and boolean numbers.

Rational numbers are what we usually think of when we think of what a number is. 1, 3, and -4.5 are all rational numbers. MATLAB stores rational numbers as doubles by default, which is a measure of the number of decimal places that are stored in each variable and thus of how accurate the values are. Note that MATLAB represents irrational numbers such as pi with rational approximations, except when using the symbolic math toolbox. See that section for details.

Boolean numbers are either "TRUE" or "FALSE", represented in MATLAB by a 1 and a 0 respectively. Boolean variables in MATLAB are actually interchangable with doubles, in that boolean operators can be performed with arrays of doubles and vice versa. Any non-zero number in this case is considered "TRUE".

Most of the rational operators also work with complex numbers. Complex numbers, however, cannot be interchanged with boolean values like the real rationals can. See the complex numbers section for details on how to use them.

[edit] Rational Operators on Single Values

MATLAB has all the standard rational operators. It is important to note, however, that Unless told otherwise, all rational operations are done on entire arrays, and use the matrix definitions. Thus, even though for now we're only talking about operations on a single value, when we get into arrays, it will be important to distinguish between matrix and componentwise multiplication, for example.

Add, Subtract, multiply, divide, exponent operators:

%addition
a = 1 + 2
%subtraction
b = 2 - 1
%matrix multiplication
c = a * b
%matrix division (pseudoinverse)
d = a / b
%exponentiation
e = a ^ b

The modulo function returns the remainder when the arguments are divided together, so a modulo b means the remainder when a is divided by b.

%modulo
remainder = mod(a,b)

All of these functions except for the modulus work for complex numbers as well.

[edit] Boolean Operators on Single Values

The boolean operators are & (boolean AND) | (boolean OR) and ~ (boolean NOT /negation). A value of zero means false, any non-zero value (usually 1) is considered true.

Here's what they do:

>>%boolean AND
>> y = 1 & 0
y = 0
>> y = 1 & 1
y = 1
>>%boolean OR
>> y = 1 | 0
y = 1
>> y = 1 | 1
y = 1

The negation operation in MATLAB is given by the symbol ~, which turns any FALSE values into TRUE and vice versa:

>> c = (a == b)
c = 1
>> ~c
ans = 0

This is necessary because conditionals (IF/SWITCH/TRY) and loops (FOR/WHILE) always look for statements that are TRUE, so if you want it to do something only when the statement is FALSE you need to use the negation to change it into a true statement.

The NOT operator has precedence over the AND and OR operators in MATLAB unless the AND or OR statements are in parenthesis:

>> y = ~1 & 0
y = 0
>> y = ~(1&0)
y = 1

[edit] Relational Operators

Equality '==' returns the value "TRUE" (1) if both arguments are equal. This must not be confused with the assignment operator '=' which assigns a value to a variable.

>> %relational 
>>a=5;b=5;
>>a==b
ans = 1
%Assignment
>>a=5;b=3;
>>a=b
a = 3

Note that in the first case, a value of 1 (true) is returned, however for the second case a gets assigned the value of b.

Greater than, less than and greater than or equal to, less than or equal to are given by >, <, >=, <= respectively. All of them return a value of true or false. Example:

>>a=3;b=5;
>>a<=b
ans = 1
>>b<a
ans = 0

[edit] Terminology

Matlab refers to Booleans as "logicals" and does not use the word "Boolean" in code or documentation.