MATLAB Matrix and Array Operations 5 — Questions and Answers
Question 1: What does `kron(A, B)` compute in MATLAB?
- Element-wise product of A and B
- The Kronecker tensor product of A and B (Correct answer)
- The cross product of two vectors
- The outer product restricted to square matrices
Correct answer: The Kronecker tensor product of A and B
`kron(A, B)` computes the Kronecker product, replacing each element of A with that scalar times the entire matrix B.
Question 2: Which MATLAB function computes the rank of a matrix?
- det(A)
- trace(A)
- rank(A) (Correct answer)
- norm(A)
Correct answer: rank(A)
`rank(A)` returns the numerical rank of A, estimated as the number of singular values above a tolerance.
Question 3: What is the output of `triu([1 2 3; 4 5 6; 7 8 9])`?
- [1 0 0; 4 5 0; 7 8 9]
- [1 2 3; 0 5 6; 0 0 9] (Correct answer)
- [1 0 0; 0 5 0; 0 0 9]
- [0 2 3; 0 0 6; 0 0 0]
Correct answer: [1 2 3; 0 5 6; 0 0 9]
`triu` returns the upper triangular part of a matrix, keeping elements on and above the main diagonal.
Question 4: What does `repmat(A, 2, 3)` do to matrix A?
- Resizes A to 2×3
- Tiles A into a 2×3 block grid (Correct answer)
- Repeats each element 2 rows and 3 columns
- Creates a random 2×3 matrix based on A
Correct answer: Tiles A into a 2×3 block grid
`repmat(A, m, n)` replicates A in an m×n tiling pattern to create a larger matrix.
Question 5: In MATLAB, what does `norm(v)` compute for a vector v by default?
- The L1 norm (sum of absolute values)
- The L2 norm (Euclidean length) (Correct answer)
- The L-infinity norm (max absolute value)
- The sum of all elements
Correct answer: The L2 norm (Euclidean length)
By default, `norm(v)` computes the 2-norm (Euclidean norm), the square root of the sum of squared elements.
Question 6: What does `any(A, 2)` return for a matrix A?
- True if any element of A is nonzero
- A column vector: true for each row that has at least one nonzero element (Correct answer)
- A row vector: true for each column that has at least one nonzero element
- The indices of nonzero elements
Correct answer: A column vector: true for each row that has at least one nonzero element
`any(A, 2)` operates along dimension 2 (columns), returning a column vector indicating rows with at least one nonzero element.
Question 7: Which expression correctly extracts the sub-matrix from rows 2–3 and columns 1–2 of a 4×4 matrix A?
- A(2:3, 1:2)
- A([2,3], [1,2])
- Both A(2:3, 1:2) and A([2,3], [1,2]) work (Correct answer)
- A{2:3, 1:2}
Correct answer: Both A(2:3, 1:2) and A([2,3], [1,2]) work
Both range indexing `2:3` and index vector `[2,3]` select the same rows and columns, so both expressions are equivalent.
What does `kron(A, B)` compute in MATLAB?