MATLAB Matrix and Array Operations 3 — Questions and Answers
Question 1: What does `eye(4)` create in MATLAB?
- A 4×4 matrix of all ones
- A 4×4 identity matrix (Correct answer)
- A 4-element vector of zeros
- A 4×4 matrix of random values
Correct answer: A 4×4 identity matrix
`eye(n)` creates an n×n identity matrix with ones on the main diagonal and zeros elsewhere.
Question 2: What is the result of `reshape([1 2 3 4 5 6], 2, 3)` in MATLAB?
- [1 2 3; 4 5 6]
- [1 3 5; 2 4 6] (Correct answer)
- [1 2; 3 4; 5 6]
- An error
Correct answer: [1 3 5; 2 4 6]
MATLAB fills matrices column-by-column, so reshape fills column 1 with [1;2], column 2 with [3;4], column 3 with [5;6].
Question 3: What does the `diag(A)` function return when A is a square matrix?
- A diagonal matrix with A's diagonal elements
- A vector of A's main diagonal elements (Correct answer)
- The trace of A
- A matrix with zeros off the diagonal
Correct answer: A vector of A's main diagonal elements
When passed a matrix, `diag` extracts the main diagonal as a column vector.
Question 4: Which MATLAB function computes the determinant of a square matrix?
- trace(A)
- rank(A)
- det(A) (Correct answer)
- inv(A)
Correct answer: det(A)
`det(A)` computes the determinant of square matrix A.
Question 5: What does `A'` (apostrophe) do to a complex matrix A in MATLAB?
- Transposes without conjugation
- Returns the complex conjugate transpose (Hermitian) (Correct answer)
- Returns only the imaginary part
- Computes the inverse
Correct answer: Returns the complex conjugate transpose (Hermitian)
The apostrophe `'` operator computes the complex conjugate transpose; for real matrices it is the same as a plain transpose.
Question 6: What is returned by `sum([1 2 3; 4 5 6])` in MATLAB?
- 21
- [5 7 9] (Correct answer)
- [6; 15]
- [1 2 3 4 5 6]
Correct answer: [5 7 9]
By default `sum` operates along the first dimension (columns), returning a row vector of column sums [5 7 9].
Question 7: Which MATLAB syntax creates a 3×3 matrix of random integers between 1 and 10?
- rand(3,3)*10
- randi(10, 3, 3) (Correct answer)
- randint(3,3,10)
- random(3,3,10)
Correct answer: randi(10, 3, 3)
`randi(imax, m, n)` generates an m×n matrix of uniformly distributed random integers between 1 and imax.
What does `eye(4)` create in MATLAB?