Computational Methods in
Chemical Engineering
LECTURE 4
CELLS,
RELATIONAL AND LOGICAL
OPERATIONS
ÖZGE KÜRKÇÜOĞLU-LEVİTAS
Cell arrays
Cell arrays are containers of all kinds of data: vectors, matrices,
strings, structures, other cell arrays, functions.
A cell is created by putting different types of
objects in curly brackets { }, e.g.,
c = {A, B, C, D}; % 1x4 cell
c = {A; B; C; D}; % 4x1 cell
c = {A, B; C, D}; % 2x2 cell
where A,B,C,D are arbitrary objects
c{i,j} accesses the data in i,j cell
c(i,j) is the cell object in the i,j position
>> A = {'Apple'; 'IBM'; 'Microsoft'}; % cells
>> B = [1 2; 3 4]; % matrix
>> C = @(x) x.^2 + 1; % function
>> D = [10 20 30 40 50]; % row
>>c = {A,B;C,D} % define 2x2 cell array
>>cellplot(c)
>> celldisp(c)
c{1,1}{1} = Cell indexing
Apple
*
c{1,1}{2} =
IBM >>c{1,1}
c{1,1}{3} = ans =
Microsoft 'Apple'
'IBM'
c{2,1} = 'Microsoft'
@(x)x.^2+1
c{1,2} =
12
34 -{2,23(3)
c{2,2} = âns=
10 20 30 40 50 so
>> c{1,1}{3}
ans =
Microsoft
>> c{1,1}{3}(6)
ans =
s
>> x = [1 2 3];
>> c{2,1}(x)
ans =
2 5 10
>> fplot(c{2,1},[0,3]);
Logical Vectors
• One of most powerful & elegant features of MATLAB
• Try these exercises on the command line:
1. Enter the following statements:
r = 1;
r <= 0.5
It returns the value 0
2. Now enter the expression
r >= 0.5
It returns the value 1
• A logical expression in Matlab involving only scalars returns a
value of 0 if it is FALSE
1 if it is TRUE
Logical Vectors
3. Enter the following statements:
r = 1:5;
r <= 3
• The logical expression r <= 3 returns a vector:
11100
• For each element of r for which r <= 3 is true,
1 is returned;
• Otherwise
0 is returned
4. Now enter r == 4
• Return: ???
Logical Vectors
• When a vector is involved in a logical expression:
Comparison is carried out element by element
• If comparison is true for a particular element of vector,
resulting vector called logical vector has 1 in corresponding
position
• Otherwise it has 0
• The same applies to logical expressions involving matrices
Logical Vectors
• You can also compare vectors with vectors in logical expressions.
• Enter the following statements:
a = 1:5;
b = [0 2 3 5 6];
a == b
• The logical expression a == b returns the logical vector
01100
• It is evaluated element by element,
• i.e. a(1) is compared with b(1), a(2) with b(2), etc.
Relational Operators
• Perform element-by-element comparison between arrays
• Return a logical array of same size, with elements set to true (1)
and false (0).
Operation Symbol Example
Less than < x<y
Less than equal to <= a < = 22
Equal to == x = = 100
Not equal to ~= x ~ = 10
Greater than equal to >= pi > = 3
Greater than > c > 100
• Note: “=” is 2nd character. =<, => and =~ are not valid operators
Logical Operators
• Logical expressions can be constructed
• not only from the six relational operators
• but also from the three logical operators
• ~ Not : 1 operand
• & And : 2 operands
• | Or : 2 operands
• The OR operator | is technically an inclusive OR,
• It is true when either or both of its operands are true.
• Logical operators have numbers as operands
• Nonzero number: true
• Zero number: false
A & B & ...
and, & and(A, B)
Ex: if matrix A is:
If matrix B is: An element of the output
array is set to 1 if all input
arrays contain a nonzero
element at that same array
location. Otherwise, that
Then, A & B is: element is set to 0.
not, ~ ~A or not(A)
Ex: if matrix A is:
An element of the output
array is set to 1 if the input
array contains a zero value
Then, ~A or not(A) is: element at that same array
location. Otherwise, that
element is set to 0.
A | B | ...
or, | or(A, B)
Ex: if matrix A is:
An element of the
output array is set to 1
if any input arrays
contain a nonzero
If matrix B is: element at that same
array location.
Otherwise, that
element is set to 0.
Then, A | B is:
Logical Operators
The precedence levels of the logical operators, among others,
Logical Operators
• As usual, precedences may be overridden with parantheses
• ˜ 0 & 0 returns a 0
• ˜ (0 & 0) returns a 1
• It is never wrong to use parantheses to make the logic clearer,
even if they are syntactically unnecessary.
Logical Operators
• You may enter an expression like
2>1&0
• MATLAB (a) accepts it & (b) returns a value of 0
• Type mathematical inequality 0 < r < 1 into Matlab
• Suppose r has the value 0.5.
• Mathematically, it is true for this value of r
• However, expression 0 < r < 1 is evaluated as 0
• First the left-hand operation (0 < 0.5) is evaluated to 1
• Then 1 < 1which is 0
• Inequalities like this should rather be coded as
(0 < r) & (r < 1)
Logical Operators and Vectors
• The logical operators can also operate on vectors (of the same
size), returning logical vectors,
˜(˜[1 2 0 -4 0])
• Replaces all the non-zeros by 1’s
• Leaves the 0’s untouched
• Most operators may not appear directly next to each other (e.g. + ˜)
• If necessary separate them with brackets
Logical Functions
• MATLAB has a number of useful logical functions that operate
on scalars, vectors and matrices.
• Some examples:
any(x) : returns 1 if any element of x is non-zero
all(x) : returns 1 if all elements of x are non-zero
xor(a,b) : returns 1 if one operand is true and the
other is false. Exclusive or
• MATLAB has a number of logical functions starting with the
characters «is».
• See is* in the Help index for the complete list.
xor C = xor(A, B)
C = xor(A, B) performs an exclusive OR operation on the
corresponding elements of arrays A and B.
The resulting element C(i,j,...) is logical true (1) if A(i,j,...) or B(i,j,...),
but not both, is nonzero.
A B C
Zero Zero 0
Zero Nonzero 1
Nonzero Zero 1
nonzero nonzero 0
Logical functions
• find(x): returns a vector containing the subscripts of the non-zero
elements of x
• For example,
a = a(find(a)) removes all the zero elements from a.
• Another use of “find” is in finding subscripts of largest (or
smallest) elements in a vector, when there is more than one.
• Enter the following: x = [8 1 -4 8 6];
find(x >= max(x))
• This returns vector [1 4], which are the subscripts of the largest
element 8.
• It works because logical expression x >= max(x) returns a
logical vector with 1’s only at positions of the largest elements.