โ† All MATLAB Flashcard Decks

Matrix and Array Operations Flashcards

6 cards from real MATLAB practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 6 Matrix and Array Operations flashcards as text
  1. Given two 3x3 matrices, A and B, which MATLAB command will perform element-wise multiplication?

    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]

  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?

    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]

  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?

    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.

  4. Given a complex matrix `C = [1+2i, 3-1i; 4, 5+6i]`, what is the result of the command `C.'`?

    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]

  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)?

    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.

  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`?

    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]