MATLAB Matrix and Vector Operations 5 — Questions and Answers
Question 1: What does `A(end, :)` return for a matrix A?
- The last element of A
- The last row of A (Correct answer)
- The last column of A
- The element at position (end, end)
Correct answer: The last row of A
The colon : selects all columns, and end refers to the last row index, returning the entire last row.
Question 2: How do you horizontally concatenate two matrices A and B with the same number of rows in MATLAB?
- [A; B]
- [A, B] (Correct answer)
- horzcat_m(A, B)
- concat(A, B, 'h')
Correct answer: [A, B]
[A, B] or [A B] concatenates A and B side by side, requiring equal row counts; equivalently horzcat(A,B).
Question 3: What does `trace(A)` return for a matrix A?
- The determinant of A
- The sum of diagonal elements (Correct answer)
- The rank of A
- The Frobenius norm
Correct answer: The sum of diagonal elements
trace returns the sum of the main diagonal elements of a square matrix.
Question 4: What is the result of `[1 2 3] > 2` in MATLAB?
- 1
- [0 0 1]
- [false false true]
- Both B and C are correct (Correct answer)
Correct answer: Both B and C are correct
MATLAB returns a logical array [0 0 1] (equivalently [false false true]) where the condition is satisfied.
Question 5: Which MATLAB function sorts a matrix A along its columns in ascending order?
- sort(A)
- sort(A, 1)
- sort(A, 2)
- Both A and B (Correct answer)
Correct answer: Both A and B
sort(A) and sort(A, 1) both sort each column independently in ascending order (dimension 1 = along rows).
Question 6: What does `max(max(A))` compute for a matrix A?
- The maximum of each column
- The overall maximum element (Correct answer)
- The maximum of each row
- An error — max requires a vector
Correct answer: The overall maximum element
The inner max finds the maximum of each column (a row vector), then the outer max finds the maximum of those maxima.
Question 7: In MATLAB, what is `linspace(0, 1, 5)`?
- [0 0.25 0.5 0.75]
- [0 0.25 0.5 0.75 1] (Correct answer)
- [0 0.2 0.4 0.6 0.8 1]
- [0.2 0.4 0.6 0.8 1]
Correct answer: [0 0.25 0.5 0.75 1]
linspace(0, 1, 5) generates 5 equally spaced points from 0 to 1 inclusive: [0, 0.25, 0.5, 0.75, 1].
What does `A(end, :)` return for a matrix A?