MATLAB Matrix and Vector Operations 2 — Questions and Answers
Question 1: What does the MATLAB expression `A \ b` compute when A is a square matrix?
- Element-wise division of A by b
- The solution x to the linear system A*x = b (Correct answer)
- The left quotient b divided by A element-wise
- The inverse of A multiplied by b (always fails)
Correct answer: The solution x to the linear system A*x = b
The backslash operator solves the linear system A*x = b using efficient factorization, equivalent to inv(A)*b but numerically more stable.
Question 2: Which MATLAB function returns both eigenvalues and eigenvectors of a matrix A?
- eig(A) with one output
- [V, D] = eig(A) (Correct answer)
- eigvec(A)
- spectrum(A)
Correct answer: [V, D] = eig(A)
With two output arguments, eig returns V (eigenvectors as columns) and D (diagonal matrix of eigenvalues).
Question 3: What is the result of `dot([1 2 3], [4 5 6])` in MATLAB?
- [4 10 18]
- 32 (Correct answer)
- 15
- 12
Correct answer: 32
The dot product is 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32.
Question 4: How do you extract the diagonal elements of matrix A as a column vector in MATLAB?
- A.diag
- diag(A) (Correct answer)
- diagonal(A)
- A(:,1:end)
Correct answer: diag(A)
diag(A) extracts the main diagonal of matrix A and returns it as a column vector.
Question 5: What does `rank(A)` return for a 4x4 identity matrix?
- 1
- 0
- 4 (Correct answer)
- 16
Correct answer: 4
The identity matrix has 4 linearly independent rows and columns, so its rank is 4 (full rank).
Question 6: Which operation computes the Hadamard (element-wise) product of two matrices A and B in MATLAB?
- A * B
- A .* B (Correct answer)
- A @ B
- hadamard(A, B)
Correct answer: A .* B
The .* operator performs element-wise multiplication, requiring A and B to have identical dimensions.
Question 7: What does `triu(A)` return for a matrix A?
- The lower triangular part of A
- The upper triangular part of A (Correct answer)
- The trace of A
- A transposed upward
Correct answer: The upper triangular part of A
triu extracts the upper triangular portion of matrix A, setting all elements below the main diagonal to zero.
What does the MATLAB expression `A \ b` compute when A is a square matrix?