MATLAB Elementwise Boolean Operations

Engineering measurements often arrive as arrays, and each measurement may need its own true-or-false decision. MATLAB elementwise Boolean operations combine conditions separately at each array position. You can use them to identify readings that meet several limits, trigger either of two warnings, or exclude flagged measurements while preserving the association between each reading and its decision.

One decision at each position

A logical array contains Boolean values: logical 1 means true and logical 0 means false. Comparisons such as T < 75 produce logical arrays. For finite real numeric inputs used directly in Boolean operations, zero is false and every nonzero value is true, including negative values. The magnitude of a nonzero input does not affect its truth value.


The operator A & B means AND. When the arrays have the same size, its governing rule is


Cij={1,Aij≠0 and Bij≠0,0,otherwise.C_{ij}=\begin{cases} 1, & A_{ij}\ne 0\text{ and }B_{ij}\ne 0,\\ 0, & \text{otherwise}. \end{cases}


The operator A | B means inclusive OR: either input, including both together, makes the result true. The operator ~A means NOT and reverses each truth value. The function xor(A,B) means exclusive OR: exactly one input must be true.


Input p

Input q

AND

OR

XOR

NOT p

0

0

0

0

0

1

0

1

0

1

1

1

1

0

0

1

1

0

1

1

1

1

0

0


For example, A = logical([1 0 1]) and B = logical([1 1 0]) give A & B equal to [1 0 0]. The result has logical type and retains three separate decisions.


Three aligned input pairs produce a three-element AND result.


Build a mask from complete conditions

A logical mask is a logical array whose true entries identify positions satisfying a rule. Suppose T stores temperatures in degrees Celsius and fault is a same-sized logical array that is true for flagged readings. An acceptable reading might require a temperature from 20 degrees Celsius through 70 degrees Celsius, inclusive, with no fault:


MATLAB
acceptable = (T >= 20) & (T <= 70) & ~fault;


Each comparison supplies one condition. AND requires all three conditions at the same position to be true. Use OR when either condition is sufficient, and XOR when both being true should be excluded.



Parentheses make the intended grouping visible. MATLAB gives & higher precedence than |, so A | B & C means A | (B & C). Write (A | B) & C when condition C must restrict both alternatives. To negate an entire comparison, write ~(T > 70).

Check dimensions before combining

Same-sized arrays combine position by position. A scalar can combine with every entry of an array. More generally, current MATLAB supports implicit expansion: in every dimension, the input sizes must match or one size must be 1.


This permits a three-element column and a three-element row to combine into a three-by-three matrix. That matrix tests every row-column combination; it does not pair the first readings, then the second readings, then the third readings. For paired measurements, check that both arrays have the same orientation.



The operators & and | are already elementwise; they need no leading dot. The distinct operators && and || require scalar logical conditions and support short-circuit evaluation. Use & and | to construct an array of decisions.

What to remember

  • AND requires both inputs; inclusive OR accepts either or both; XOR accepts exactly one.
  • NOT reverses truth values, and nonzero numeric inputs count as true.
  • Combine complete comparisons and use parentheses to express the intended grouping.
  • Check array sizes and orientations before interpreting a mask.

Example: Screen Motor Test Readings

A motor test records six paired measurements. The array T contains winding temperatures in degrees Celsius, I contains currents in amperes, and fault is true when the corresponding reading is flagged. Each position represents one recorded sample.


MATLAB
T = [62 77 81 69 74 85];
I = [8.2 9.5 7.8 10.4 8.9 11.2];
fault = logical([0 0 0 1 0 0]);


A sample is acceptable when its temperature is below 80 degrees Celsius, its current is at most 10 A, and it has no fault flag.


a) Write an elementwise expression for the acceptance mask and evaluate it.


All three requirements must be true for the same sample. Begin by evaluating each condition separately, preserving the order of the six measurements:


MATLAB
cool = T < 80;
% [1 1 0 1 1 0]
withinCurrent = I <= 10;
% [1 1 1 0 1 0]
noFault = ~fault;
% [1 1 1 0 1 1]


Use elementwise AND because acceptance requires all three conditions:


MATLAB
acceptable = (T < 80) & (I <= 10) & ~fault;
% [1 1 0 0 1 0]


Samples 1, 2, and 5 satisfy every requirement. Sample 3 exceeds the temperature limit; sample 4 exceeds the current limit and is flagged; sample 6 exceeds both measurement limits. The acceptance mask is [1 1 0 0 1 0].


b) Ignoring fault flags for this part, identify samples with exactly one warning condition: temperature at least 80 degrees Celsius or current above 10 A.


First construct the two warning masks. The temperature warning includes 80 degrees Celsius, while the current warning requires a value strictly above 10 A:


MATLAB
hot = T >= 80;
% [0 0 1 0 0 1]
overCurrent = I > 10;
% [0 0 0 1 0 1]


Exactly one warning means exclusive OR, so use xor:


MATLAB
oneWarning = xor(hot,overCurrent);
% [0 0 1 1 0 0]


Sample 3 has only the temperature warning, and sample 4 has only the current warning. Sample 6 has both warnings, so XOR returns false there. The required mask is [0 0 1 1 0 0], identifying two samples with exactly one warning.


c) What percentage of all recorded samples is acceptable? Report the result to one decimal place.


The acceptance mask from part a contains ________ true entries among ________ recorded samples. Count accepted samples in the numerator and retain all six recorded samples in the denominator:


Accepted percentage=number acceptednumber recorded×100%=36×100%=50.0%.\text{Accepted percentage}=\frac{\text{number accepted}}{\text{number recorded}}\times100\% =\frac{3}{6}\times100\%=50.0\%.


Thus, 50.0%50.0\% of the recorded samples are acceptable. This percentage describes the recorded samples; no assumption about the time between measurements is needed.

Two MATLAB arrays contain unitless numerical status codes for four channels:


MATLAB
A = [0 -4 6 0];
B = [9 2 0 0];
result = A & ~B;


Which logical array does result contain?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A pressure test records four readings in kPa and a matching fault flag for each reading:


MATLAB
p = [170 180 230 260];
fault = logical([0 0 1 0]);


A reading is acceptable when its pressure is at least 180 kPa, below 260 kPa, and its fault flag is false. Which expression produces one logical decision per reading according to this rule?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A hydraulic test records seven pressure samples in kPa. A true flag marks a sample that must be excluded:


MATLAB
p = [90 140 180 230 290 340 410];
flag = logical([0 0 1 0 0 1 0]);


An acceptable sample has pressure from 140 kPa through 340 kPa, inclusive, and has a false flag. What percentage of all seven recorded samples is acceptable? Answer in percent (%), to one decimal place, without the percent symbol.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice