A useful MATLAB experiment often begins by selecting observations. The mathematical condition is only the first part of the task: we must also decide whether an observation is a single array entry, a row of paired measurements, or a position in a grid. A correct comparison applied at the wrong level can quietly answer the wrong question.
The main examples use finite, real double arrays; one policy discussion explicitly allows non-finite values. They use base MATLAB syntax. Work through the shapes and predicted values before running them, and then compare the displayed result.
A mask records a question at each position
Definition 12.1.1
(Logical mask and column-major linear index)
For an array A, the expression A > 3 is a logical mask: an array of the same size whose entry is true exactly when the corresponding entry of A exceeds three. The expression A(mask) selects the entries at the true positions. MATLAB numbers the entries of an array by a linear index that runs down the first column, then down the second, and so on (column-major order).
Proposition 12.1.2
(Row and column from the linear index)
In an array the entry in row and column has linear index . Conversely, for , its row is and its column is .
Proof
(Counting entries column by column)
Columns each contain entries and are all visited before column , so entries precede it; within column , the entry in row is the th visited. This gives with . Then with , so is the remainder and the quotient of on division by .
Worked example 12.1.3
(Selecting by a mask returns a column in column-major order)
Consider
A = [2 5 1; 4 3 6];
mask = A > 3;
selected = A(mask);
The mask and selected values are
mask = [0 1 0; 1 0 1] (logical)
selected = [4; 5; 6]
Here . The linear positions visited are 1, 2 in column one, then 3, 4 in column two, then 5, 6 in column three. The true positions are 2, 3 and 6, and the proposition recovers their coordinates: gives , . This explains why the result begins with 4 rather than 5. The two-dimensional coordinates of the selected entries are , and .
For a matrix mask mask, [row,col] = find(mask) returns matching row and column indices; find(mask) with one output returns linear indices. With multiple outputs and a two-dimensional array, pairing row(j) with col(j) identifies one selected position. For higher-dimensional arrays, the interpretation of multiple find outputs requires extra care, so the examples below remain two-dimensional.
Conditions combine element by element
Let B = [1 4 7; 2 5 8]. To select values at least 2 and less than 7, use
keep = (B >= 2) & (B < 7);
B(keep)
The answer is [2; 4; 5] in linear order. Parentheses make the two comparisons visible. The operator & combines corresponding logical entries; | means elementwise or, and ~ means not. The scalar short-circuit operators && and || are intended for scalar logical conditions, such as a single if decision; they do not replace an elementwise mask over a whole matrix.
If invalid measurements can occur, formulate the policy explicitly. If double measurements may be non-finite, (isfinite(B) & B >= 2) excludes NaN, Inf and -Inf before selection. Omitting a row, imputing a measurement and reporting an error are distinct data policies; the comparison itself does not decide which is appropriate.
Select rows when rows are observations
Worked example 12.1.4
(Keeping whole rows of paired measurements)
Suppose columns of D represent time, position and quality:
D = [0 10 1; 1 12 0; 2 15 1; 3 18 1];
keep = D(:,3) == 1;
valid = D(keep,:);
Here D(:,3) has four rows, so keep asks one question per observation. D(keep,:) keeps entire rows and all columns:
valid = [0 10 1; 2 15 1; 3 18 1]
The time and position from each retained row stay paired. In contrast, D(D > 1) selects individual entries and returns a vector; it cannot by itself describe which time belongs to which position. Also, D(:,3) == 1 is not the same as D(:,1) > 1: the first uses quality, the second uses time.
A common guard is if any(keep) before calculating a statistic that requires at least one observation. A nonempty selected set is an existence check, not a guarantee that its observations are independent or representative.
Common mistake
(Selecting entries when whole rows are needed)
With D = [1 5; 2 -1; 3 8; 4 0], the call D(D(:,2) > 0) uses a mask on a array, so it selects by linear position among the first four entries, which are the identifiers; the result is the vector [1;3], not the rows [1 5; 3 8]. The mask must go in the row position, D(D(:,2) > 0, :), to keep whole rows.
A grid has two coordinate arrays
Worked example 12.1.5
(A mask on a meshgrid)
Suppose
x = [-1 0 1];
y = [0 2];
[X,Y] = meshgrid(x,y);
Z = X.^2 + Y;
mask = Z >= 2;
meshgrid produces two arrays of size :
X = [-1 0 1; -1 0 1]
Y = [ 0 0 0; 2 2 2]
Z = [ 1 0 1; 3 2 3]
The selected grid points are obtained together by X(mask), Y(mask) and Z(mask). Their vectors are respectively [-1;0;1], [2;2;2] and [3;2;3]. Their th entries refer to the same point because they use the same logical mask and ordering.
A grid records sampled points, not every point of the continuous region. A plotted surface interpolates or connects a finite collection of samples in a renderer; it does not establish a theorem about unsampled values. Record the grid, axis labels, and any filtering rule when presenting the figure.
Counterexample mode
Matrix power instead of elementwise power
Claim: X^2 squares every entry of X. For the square matrix X = [1 2; 3 4] the call runs without error, but X^2 is the matrix product , whereas X.^2 is . For the grid array above X^2 is not even dimensionally valid, so the mistake is visible only on square data. Use the dotted operator whenever a function is applied pointwise.
Practice and solutions
Quick check
(Entry selection. Let C = [4 0; -1 4; 2 7]. Predict find(C == 4) and C(C >= 2).)
List the entries in column-major order first.
Solution · Answer
The linear column order is 4, -1, 2, 0, 4, 7. The two fours occur at linear indices [1;5]. Values at least two are [4;2;4;7]. Their order reflects columns, not row-by-row reading.
Quick check
(Rows. Let D = [1 5; 2 -1; 3 8; 4 0], where the first column is an identifier and the second a measurement. Select whole rows with a positive measurement and compute their mean measurement.)
Put the mask in the row position.
Solution · Answer
keep = D(:,2) > 0; E = D(keep,:); gives E = [1 5; 3 8]. Then mean(E(:,2)) is . Computing mean(E(:)) would instead average identifiers and measurements together, a different quantity.
Quick check
(Index coordinates. On the grid in section 4, give [row,col] = find(Z >= 2) and explain how it locates the selected points.)
Look at which row of Z contains the true entries.
Solution · Answer
The true entries occupy row 2 of each column. Thus row = [2;2;2], col = [1;2;3]; pairing entries gives , and . These are array indices, while the corresponding physical coordinates are , and . An array index is not automatically an or value.
Quick check
(Empty selection. If no entry of D(:,2) is positive, what should a script do before reporting a positive-measurement mean?)
Think about what mean would be asked to average.
Solution · Answer
First compute the mask and check whether it contains a true entry. If not, report that no observation satisfies the condition or use another explicitly specified policy; a numerical summary cannot stand in for an absent positive observation. For example:
keep = D(:,2) > 0;
if any(keep)
positive_mean = mean(D(keep,2));
fprintf('Mean of selected measurements: %.6g\n', positive_mean);
else
fprintf('No positive measurements.\n');
end