MATLAB Matrix and Vector Operations Questions and Answers — Questions and Answers
Question 1: Given two matrices, A = [1 2; 3 4] and B = [5 6; 7 8], which of the following MATLAB commands will produce a matrix C where each element is the product of the corresponding elements in A and B (i.e., C(1,1) = A(1,1)*B(1,1), C(1,2) = A(1,2)*B(1,2), etc.)?
- C = A * B
- C = A x B
- C = A .* B (Correct answer)
- C = matrix_multiply(A, B)
Correct answer: C = A .* B
In MATLAB, the `.*` operator performs element-wise multiplication. The `*` operator performs standard matrix multiplication, which requires the inner dimensions of the matrices to agree and calculates the dot product of rows and columns. The `x` is not a valid multiplication operator, and `matrix_multiply` is not a standard built-in function.
Question 2: A user defines a matrix M = [10 20 30; 40 50 60; 70 80 90]. Which command will extract the entire second row of M?
- M(2)
- M(2, :) (Correct answer)
- M(:, 2, :)
- M(row=2)
Correct answer: M(2, :)
In MATLAB, to select all elements in a specific dimension, you use the colon operator `:`. The syntax `M(2, :)` specifies the element at the second row and all columns. `M(2)` would use linear indexing and return the second element of the matrix when treated as a single column (which is 40). `M(:, 2, :)` is invalid syntax, and `M(row=2)` is not valid MATLAB syntax for indexing.
Question 3: You have two vectors, V1 = [1; 2; 3] and V2 = [4; 5; 6]. You need to calculate the scalar dot product. Which of the following commands is the standard and most direct way to achieve this in MATLAB?
- V1' * V2
- V1 .* V2
- cross(V1, V2)
- dot(V1, V2) (Correct answer)
Correct answer: dot(V1, V2)
The `dot()` function is the dedicated function in MATLAB for calculating the scalar dot product of two vectors. While `V1' * V2` (transpose of V1 multiplied by V2) would also compute the dot product for these column vectors, `dot()` is the more explicit and standard function for this operation. `V1 .* V2` performs element-wise multiplication, resulting in a vector, and `cross(V1, V2)` calculates the cross product, which also results in a vector.
Question 4: Consider the command `C = [A; B]`. What condition must matrices A and B satisfy for this command to execute without error?
- A and B must have the same number of rows.
- A and B must be square matrices.
- A and B must have the same number of columns. (Correct answer)
- A and B must have the same number of total elements.
Correct answer: A and B must have the same number of columns.
The semicolon `;` in matrix construction is used for vertical concatenation, stacking matrices on top of each other. For this to be possible, the matrices must align vertically, which means they must have the same number of columns. Having the same number of rows is required for horizontal concatenation (e.g., `[A, B]`).
Question 5: Given a vector `V = [5, 12, 3, 18, 9, 15]`, which of the following commands will return a logical vector indicating which elements of V are greater than 10?
- find(V > 10)
- V > 10 (Correct answer)
- V(V > 10)
- select(V > 10)
Correct answer: V > 10
In MATLAB, performing a relational operation like `V > 10` on a vector or matrix directly results in a logical array of the same size, with `1` (true) where the condition is met and `0` (false) where it is not. The `find` command returns the indices of non-zero elements, not the logical vector itself. `V(V > 10)` uses logical indexing to extract the values that meet the condition. `select` is not a standard MATLAB function for this purpose.
Question 6: What is the result of the expression `[1 2 3] * [4; 5; 6]` in MATLAB?
- An error, because the matrix dimensions do not agree.
- The vector [4 10 18].
- The scalar value 32. (Correct answer)
- The matrix [4 5 6; 8 10 12; 12 15 18].
Correct answer: The scalar value 32.
This expression performs standard matrix multiplication. The first matrix has dimensions 1x3, and the second has dimensions 3x1. Since the inner dimensions (3 and 3) match, the multiplication is valid. The result will have the outer dimensions, which is 1x1 (a scalar). The calculation is (1*4 + 2*5 + 3*6), which equals 4 + 10 + 18 = 32.
Given two matrices, A = [1 2; 3 4] and B = [5 6; 7 8], which of the following MATLAB commands will produce a matrix C where each element is the product of the corresponding elements in A and B (i.e., C(1,1) = A(1,1)*B(1,1), C(1,2) = A(1,2)*B(1,2), etc.)?