Challenge - 5 Problems
Matrix Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this matrix indexing code?
Consider the matrix
A = [1 2 3; 4 5 6; 7 8 9]; What is the value of A(2,3)?MATLAB
A = [1 2 3; 4 5 6; 7 8 9]; result = A(2,3);
Attempts:
2 left
💡 Hint
Remember that the first index is the row number and the second is the column number.
✗ Incorrect
In MATLAB,
A(2,3) means row 2, column 3 of matrix A. The element there is 6.❓ Predict Output
intermediateWhat does this code output when extracting a submatrix?
Given
B = [10 20 30; 40 50 60; 70 80 90]; what is the output of B(1:2, 2:3)?MATLAB
B = [10 20 30; 40 50 60; 70 80 90]; sub = B(1:2, 2:3);
Attempts:
2 left
💡 Hint
The colon operator selects ranges of rows and columns.
✗ Incorrect
Rows 1 and 2, columns 2 and 3 of B are selected, resulting in the 2x2 matrix [20 30; 50 60].
❓ Predict Output
advancedWhat is the output of this linear indexing code?
Given
C = [3 6 9; 12 15 18; 21 24 27]; what is the value of C(5)?MATLAB
C = [3 6 9; 12 15 18; 21 24 27]; val = C(5);
Attempts:
2 left
💡 Hint
MATLAB stores matrices column-wise when using single index.
✗ Incorrect
Linear indexing counts down columns first. The 5th element is in column 2, row 2, which is 15.
❓ Predict Output
advancedWhat error does this code produce?
What happens when you run this code?
D = [1 2; 3 4]; val = D(3,3);MATLAB
D = [1 2; 3 4]; val = D(3,3);
Attempts:
2 left
💡 Hint
Check the size of matrix D and the indices used.
✗ Incorrect
Matrix D is 2x2, so index (3,3) is outside its size, causing an indexing error.
🧠 Conceptual
expertHow many elements are in the matrix after this indexing operation?
Given
E = magic(4); what is the number of elements in F = E(2:4, 1:3);?MATLAB
E = magic(4); F = E(2:4, 1:3); num_elements = numel(F);
Attempts:
2 left
💡 Hint
Count the rows and columns selected and multiply.
✗ Incorrect
Rows 2 to 4 means 3 rows, columns 1 to 3 means 3 columns, so 3*3=9 elements.
