Free MATLAB Matrix and Array Operations Questions and Answers — Questions and Answers
Question 1: Given two 3x3 matrices, A and B, which MATLAB command will perform element-wise multiplication?
- A * B
- A .* B (Correct answer)
- A x B
- multiply(A, B)
Correct answer: A .* B
In MATLAB, the `.*` operator is used for element-wise multiplication, where each element in the first matrix is multiplied by the corresponding element in the second matrix. The `*` operator performs standard matrix multiplication, which follows the rules of linear algebra. [17, 19, 21]
Question 2: A user has a matrix `M = [10, 25, 15; 30, 5, 20];`. Which command will return a column vector containing only the elements of `M` that are greater than 15?
- M(M > 15) (Correct answer)
- find(M > 15)
- M > 15
- extract(M, M > 15)
Correct answer: M(M > 15)
This uses logical indexing. The expression `M > 15` creates a logical matrix of the same size as `M` with `true` (1) where the condition is met and `false` (0) otherwise. Using this logical matrix as an index for `M` extracts all the elements for which the logical matrix is `true`, returning them as a column vector. [4, 5, 6]
Question 3: A user wants to create a new 4x3 matrix by vertically stacking a 2x3 matrix `A` on top of another 2x3 matrix `B`. Which of the following commands will accomplish this?
- [A, B]
- [A B]
- [A; B] (Correct answer)
- cat(A, B)
Correct answer: [A; B]
In MATLAB, the semicolon (;) is used within square brackets to concatenate arrays vertically. [2, 16, 18] Commas (,) or spaces are used for horizontal concatenation. [2, 3] While the `cat` function can be used, `cat(A, B)` without a dimension argument is not the correct syntax for this operation.
Question 4: Given a complex matrix `C = [1+2i, 3-1i; 4, 5+6i]`, what is the result of the command `C.'`?
- It computes the complex conjugate transpose.
- It results in an error because the matrix is complex.
- It computes the non-conjugate transpose. (Correct answer)
- It calculates the inverse of the matrix.
Correct answer: It computes the non-conjugate transpose.
In MATLAB, there are two transpose operators. The apostrophe (`'`) performs the complex conjugate transpose (transpose and negate imaginary parts). [11] The dot-apostrophe (`.'`) performs the non-conjugate transpose, which simply interchanges rows and columns without affecting the sign of the imaginary parts. [8, 10]
Question 5: What is the key difference between the `length(M)` and `numel(M)` functions for a non-vector matrix `M` (e.g., a 3x5 matrix)?
- `length(M)` returns the number of columns, while `numel(M)` returns the number of rows.
- `length(M)` returns the total number of elements, while `numel(M)` returns the largest dimension.
- `length(M)` returns the size of the largest dimension, while `numel(M)` returns the total number of elements. (Correct answer)
- Both functions return the total number of elements and are interchangeable.
Correct answer: `length(M)` returns the size of the largest dimension, while `numel(M)` returns the total number of elements.
For a matrix, `length(M)` returns the size of the longest dimension (equivalent to `max(size(M))`). [15, 27] In contrast, `numel(M)` (short for 'number of elements') returns the total number of elements in the matrix (equivalent to `prod(size(M))`). [15, 24] For a 3x5 matrix, `length` would be 5, while `numel` would be 15.
Question 6: A user executes the following code: `A = [1 2; 3 4];` `B = [5; 6];` `C = A * B;` What are the dimensions of the resulting matrix `C`?
- 2x2
- 1x2
- An error occurs.
- 2x1 (Correct answer)
Correct answer: 2x1
This code performs standard matrix multiplication. Matrix multiplication is defined when the inner dimensions are equal. Here, `A` is 2x2 and `B` is 2x1. The inner dimensions (2 and 2) match. The resulting matrix `C` will have the outer dimensions, which are 2x1. [17, 26]
Given two 3x3 matrices, A and B, which MATLAB command will perform element-wise multiplication?