Concept Flow - Matrix indexing (row, col)
Start with Matrix
Choose Row Index
Choose Column Index
Access Element at (row, col)
Use or Display Element
End
This flow shows how to pick a row and column number to get the element from a matrix.
A = [10, 20, 30; 40, 50, 60; 70, 80, 90]; element = A(2,3);
| Step | Action | Row Index | Column Index | Accessed Element | Explanation |
|---|---|---|---|---|---|
| 1 | Create matrix A | - | - | - | Matrix A is set as 3x3 with values 10 to 90 |
| 2 | Select row index | 2 | - | - | Row 2 is chosen |
| 3 | Select column index | 2 | 3 | - | Column 3 is chosen |
| 4 | Access element A(2,3) | 2 | 3 | 60 | Element at row 2, col 3 is 60 |
| 5 | Store element in variable | 2 | 3 | 60 | Variable 'element' now holds 60 |
| Variable | Start | After Step 1 | After Step 4 | Final |
|---|---|---|---|---|
| A | undefined | [10 20 30; 40 50 60; 70 80 90] | [10 20 30; 40 50 60; 70 80 90] | [10 20 30; 40 50 60; 70 80 90] |
| element | undefined | undefined | 60 | 60 |
Matrix indexing uses (row, column) inside parentheses. Syntax: element = A(row, col); Row number picks vertical position, column picks horizontal. Indexes start at 1 in MATLAB. Accessed element is at intersection of chosen row and column. Use this to read or change matrix values.
A?A(row, column).A(2, 3).B in MATLAB?: means all elements in that dimension. To select the entire 4th row, use B(4, :).B(:, 4) selects the 4th column, B(4) is invalid for 2D matrix, and square brackets are not used for indexing.C = [1 2 3; 4 5 6; 7 8 9], what is the output of C(2, 1:3)?C(2, 1:3) means row 2, columns 1 through 3.C is [4 5 6]. Selecting columns 1 to 3 gives the entire row.D = [10 20 30; 40 50 60] and we want to select the 3rd row?row3 = D(3, :);D has 2 rows and 3 columns only.D(3, :) is invalid and causes a runtime error because that row does not exist.E = [5 10 15; 20 25 30; 35 40 45]. How do you create a new matrix F that contains only the 1st and 3rd rows and the 2nd and 3rd columns of E?E.E([1 3], [2 3]) to select these specific rows and columns.Pass the test and this pattern moves unknown