Bird
Raised Fist0
MATLABdata~10 mins

Matrix indexing (row, col) in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
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.
Execution Sample
MATLAB
A = [10, 20, 30; 40, 50, 60; 70, 80, 90];
element = A(2,3);
This code creates a 3x3 matrix and gets the element in row 2, column 3.
Execution Table
StepActionRow IndexColumn IndexAccessed ElementExplanation
1Create matrix A---Matrix A is set as 3x3 with values 10 to 90
2Select row index2--Row 2 is chosen
3Select column index23-Column 3 is chosen
4Access element A(2,3)2360Element at row 2, col 3 is 60
5Store element in variable2360Variable 'element' now holds 60
💡 Finished accessing element at row 2, column 3
Variable Tracker
VariableStartAfter Step 1After Step 4Final
Aundefined[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]
elementundefinedundefined6060
Key Moments - 2 Insights
Why do we use two numbers inside parentheses like A(2,3)?
Because the first number picks the row and the second picks the column, as shown in step 4 of the execution table.
What happens if we swap the row and column numbers like A(3,2)?
You get a different element because row 3, column 2 is a different position. The execution table shows how row and column select the element.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of the element accessed at step 4?
A30
B50
C60
D80
💡 Hint
Check the 'Accessed Element' column at step 4 in the execution table.
At which step is the matrix A created?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column to find when matrix A is set.
If we change the row index to 3 and column index to 1, what element would be accessed?
A70
B80
C40
D90
💡 Hint
Look at the matrix A in variable_tracker and find element at row 3, column 1.
Concept Snapshot
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.
Full Transcript
Matrix indexing in MATLAB means picking a row and a column number inside parentheses to get one element from a matrix. For example, A(2,3) means row 2, column 3 of matrix A. The code creates a 3 by 3 matrix A with numbers 10 to 90. Then it picks the element at row 2, column 3, which is 60. Variables change as the matrix is created and the element is stored. Beginners often wonder why two numbers are needed and what happens if they swap them. The execution table shows each step clearly. The quick snapshot reminds that MATLAB indexes start at 1 and the syntax is element = A(row, col).

Practice

(1/5)
1. In MATLAB, how do you access the element in the 2nd row and 3rd column of a matrix A?
easy
A. A(3, 2)
B. A[2, 3]
C. A(2, 3)
D. A{2, 3}

Solution

  1. Step 1: Understand MATLAB indexing syntax

    MATLAB uses parentheses with row and column indices in the form A(row, column).
  2. Step 2: Apply to given indices

    To get the element in the 2nd row and 3rd column, use A(2, 3).
  3. Final Answer:

    A(2, 3) -> Option C
  4. Quick Check:

    Matrix indexing = A(2, 3) [OK]
Hint: Remember: first index is row, second is column [OK]
Common Mistakes:
  • Swapping row and column indices
  • Using square brackets instead of parentheses
  • Using curly braces which are for cell arrays
2. Which of the following is the correct syntax to select the entire 4th row of a matrix B in MATLAB?
easy
A. B(:, 4)
B. B(4, :)
C. B(4)
D. B[4, :]

Solution

  1. Step 1: Recall how to select entire rows or columns

    In MATLAB, : means all elements in that dimension. To select the entire 4th row, use B(4, :).
  2. Step 2: Check options

    B(:, 4) selects the 4th column, B(4) is invalid for 2D matrix, and square brackets are not used for indexing.
  3. Final Answer:

    B(4, :) -> Option B
  4. Quick Check:

    Row selection = B(4, :) [OK]
Hint: Use ':' for all columns in a specific row [OK]
Common Mistakes:
  • Confusing row and column positions
  • Using square brackets for indexing
  • Omitting the colon for full dimension selection
3. Given the matrix C = [1 2 3; 4 5 6; 7 8 9], what is the output of C(2, 1:3)?
medium
A. [4 5 6]
B. [1 2 3]
C. [7 8 9]
D. [5 6 7]

Solution

  1. Step 1: Identify the row and columns selected

    C(2, 1:3) means row 2, columns 1 through 3.
  2. Step 2: Extract elements from matrix

    Row 2 of C is [4 5 6]. Selecting columns 1 to 3 gives the entire row.
  3. Final Answer:

    [4 5 6] -> Option A
  4. Quick Check:

    Row 2, cols 1-3 = [4 5 6] [OK]
Hint: Use row index first, then column range with ':' [OK]
Common Mistakes:
  • Selecting wrong row or column range
  • Confusing row and column indices
  • Expecting a single number instead of a vector
4. What is wrong with this MATLAB code snippet if D = [10 20 30; 40 50 60] and we want to select the 3rd row?
row3 = D(3, :);
medium
A. Syntax error due to wrong indexing
B. Returns an empty array
C. Returns the last row instead of the 3rd
D. Runtime error because row 3 does not exist

Solution

  1. Step 1: Check matrix size

    Matrix D has 2 rows and 3 columns only.
  2. Step 2: Analyze indexing for row 3

    Trying to access row 3 with D(3, :) is invalid and causes a runtime error because that row does not exist.
  3. Final Answer:

    Runtime error because row 3 does not exist -> Option D
  4. Quick Check:

    Index out of bounds = runtime error [OK]
Hint: Check matrix size before indexing rows or columns [OK]
Common Mistakes:
  • Assuming MATLAB returns empty for out-of-range index
  • Confusing syntax error with runtime error
  • Expecting MATLAB to auto-expand matrix
5. You have a matrix 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?
hard
A. F = E([1 3], [2 3]);
B. F = E(1:3, 2:3);
C. F = E(:, [1 3]);
D. F = E([2 3], [1 2]);

Solution

  1. Step 1: Identify required rows and columns

    We want rows 1 and 3, and columns 2 and 3 from matrix E.
  2. Step 2: Use indexing with vectors for rows and columns

    In MATLAB, use E([1 3], [2 3]) to select these specific rows and columns.
  3. Final Answer:

    F = E([1 3], [2 3]); -> Option A
  4. Quick Check:

    Index rows and columns with vectors = F = E([1 3], [2 3]) [OK]
Hint: Use vectors in parentheses to pick specific rows and columns [OK]
Common Mistakes:
  • Using colon to select unwanted rows or columns
  • Swapping row and column indices
  • Using incorrect row or column numbers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard