Modifying Arrays
MATLAB Array Indexing
MATLAB Array Indexing lets you work with selected entries of an array without replacing the entire variable. In engineering data, rows might represent sensors while columns represent measurement times. Indexing connects that physical meaning to the entries you read, correct, add, or delete. The examples below use ordinary numeric arrays of type double.
Select entries by position
MATLAB counts positions starting at 1. For a matrix, the first subscript identifies the row and the second identifies the column:
A = [12 18 24; 15 21 27; 16 22 28];
x = A(2,3);Here, x is 27: move to row 2, then column 3. The subscripts describe positions, not measured values. For numeric positional indexing, use positive integers within the existing dimensions when reading entries.
Parentheses select entries from an existing array. Square brackets assemble values or index lists. Thus, A(2,3) selects one entry, whereas [2 3] is a list containing two numbers.
Select blocks and use linear indices
A colon by itself selects every position along its dimension. A range such as 1:2 selects consecutive positions, including both endpoints here. Inside an indexing expression, end means the last position in the dimension being indexed.
Expression | Selection from the original A | Result size |
|---|---|---|
A(2,:) | Every column in row 2 | 1 by 3 |
A(:,3) | Every row in column 3 | 3 by 1 |
A(1:2,2:end) | Rows 1–2 and columns 2–3 | 2 by 2 |
A([2 1],[1 3]) | Rows 2 then 1, with columns 1 then 3 | 2 by 2 |
Two index lists select every combination of the requested rows and columns. They do not select only paired coordinates. Their order also determines the order of the returned rows and columns.
With one subscript, MATLAB uses linear indexing: it counts down the first column, then down the next column. For a matrix with m rows, row r and column c correspond to linear index k:
The preceding c−1 columns contribute (c−1)m entries. For the original A, row 2 and column 3 therefore have index 2+(3−1)(3)=8, so A(8) is 27.
Replace entries and grow arrays
An indexed assignment places a selection on the left of = and replacement values on the right:
B = A;
B(1,2) = 19;
B(:,3) = 0;
B(:,end+1) = [30; 31; 32];The first edit changes one entry. The second replaces all three entries in column 3 with zero. The last appends a column, making B a 3-by-4 matrix. Semicolons separate the three replacement values into a column.
Reading beyond an array's boundary produces an error. Assigning beyond the boundary can grow an ordinary numeric array; newly created positions that receive no explicit value are filled with zeros. Those zeros are stored numbers, so do not interpret them automatically as measured data.
Delete entries and track positions
Assigning the empty array [] removes selected entries:
B(:,2) = [];
B(1,:) = [];The first command removes column 2. The second removes row 1 from the updated matrix. Remaining rows and columns close the gaps and acquire new indices. For a vector, v([1 3]) = [] removes its current first and third entries together.
If several original columns must be removed, select them in one deletion. Separate deletions change the positions used by subsequent commands. Reevaluate end and any linear-index calculation using the array's current dimensions.
What to remember
- Use row first, column second, with counting starting at 1.
- A single subscript counts down columns.
- Indexed assignment replaces selected entries and can grow an array.
- Deletion changes both dimensions and subsequent positions.
Example: Edit a pressure-test record
Three pressure sensors record four tests. Rows represent sensors 1–3, columns represent tests 1–4, and every entry is in kilopascals. The ordinary numeric MATLAB array is
P = [102 105 108 111; 98 101 104 107; 95 99 103 106];Complete the parts in order, retaining each change for subsequent parts.
a) Extract sensor 2's readings for tests 2 through the last test. Also find the linear index and value of the reading at row 2, column 3.
Select row 2 and columns 2 through end. In the column position, end is currently ________:
readings = P(2,2:end);This returns the 1-by-3 row vector [101 104 107], with every reading in kilopascals.
For linear indexing, the matrix has three rows. Each complete preceding column contributes three entries. The position of row 2, column 3 is
Consequently, P(8) and P(2,3) select the same reading, 104 kPa.
b) Sensor 2's test 3 reading should be 104.6 kPa. Correct that entry, then append a fifth test with readings of 114, 110, and 109 kPa for sensors 1, 2, and 3. Show the updated matrix and its size.
The correction targets row 2 and column 3 without changing any dimensions:
P(2,3) = 104.6;Next, append a column. There are currently four columns, so end+1 in the column position is ________. The three readings form a column to match the three selected sensor rows:
P(:,end+1) = [114; 110; 109];The updated array is
P = [102 105 108 111 114;
98 101 104.6 107 110;
95 99 103 106 109];Every entry remains in kilopascals. The resulting size is 3 by 5: three sensors and five tests.
c) Test 2 is rejected. Delete its entire column, show the remaining matrix, and determine the value selected by P(2,end-1) after deletion.
All three sensor readings for test 2 must be removed together. Select every row and column 2, then assign the empty array:
P(:,2) = [];The remaining columns correspond, in order, to original tests 1, 3, 4, and the appended test 5:
P = [102 108 111 114;
98 104.6 107 110;
95 103 106 109];The array is now 3 by 4. In the column position, end is 4, so end-1 selects current column ________, which represents original test 4. At sensor row 2, the selected reading is
Deleting a column changes the positions of later tests, so the expression must be interpreted using the updated array.
An engineer stores displacement measurements in millimetres, with one test per row and one sensor channel per column. MATLAB executes these statements in order:
A = [6 8; 10 12; 14 16];
B = A;
C = A;
B(2,:) = 0;
C(2,:) = [];Complete the statements with exact integers describing the final arrays.
- Array B contains tests and sensor channels.
- Array C contains tests and sensor channels.
The entries of F are measured forces in newtons. MATLAB executes:
F = [11 12 13; 21 22 23; 31 32 33];
R = F([2 1],[3 1]);Which array is stored in R? Every entry shown is in newtons.