Bird
Raised Fist0
MATLABdata~20 mins

Element-wise operations (.*, ./, .^) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Element-wise Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of element-wise multiplication
What is the output of the following MATLAB code?
MATLAB
A = [1 2 3];
B = [4 5 6];
C = A .* B;
disp(C);
A[4 10 18]
B[5 7 9]
C[4 5 6]
D[1 2 3 4 5 6]
Attempts:
2 left
💡 Hint
Remember that .* multiplies each element of A by the corresponding element of B.
❓ Predict Output
intermediate
2:00remaining
Result of element-wise division
What will be displayed after running this MATLAB code?
MATLAB
X = [10 20 30];
Y = [2 4 5];
Z = X ./ Y;
disp(Z);
A[5 5 6.0000]
B[5 4 6]
C[5 5 6]
D[5 5 6.0]
Attempts:
2 left
💡 Hint
Element-wise division divides each element of X by the corresponding element of Y.
❓ Predict Output
advanced
2:00remaining
Output of element-wise power operation
What is the output of this MATLAB code snippet?
MATLAB
P = [2 3 4];
Q = [3 2 1];
R = P .^ Q;
disp(R);
A[6 5 4]
B[8 9 1]
C[8 6 4]
D[8 9 4]
Attempts:
2 left
💡 Hint
The .^ operator raises each element of P to the power of the corresponding element in Q.
❓ Predict Output
advanced
2:00remaining
Error type from wrong operator usage
What error does this MATLAB code produce?
MATLAB
A = [1 2 3];
B = [4 5 6];
C = A * B;
AError: Undefined operator '*' for input arguments of type 'double'.
BError: Inner matrix dimensions must agree.
CNo error, output is [4 10 18].
DError: Matrix dimensions must agree for element-wise multiplication.
Attempts:
2 left
💡 Hint
The * operator is matrix multiplication and requires matching inner dimensions.
🧠 Conceptual
expert
2:00remaining
Number of elements in result after element-wise operations
Given two matrices A (2x3) and B (2x3), what is the number of elements in the result of A .* B?
A6
B2
C3
D1
Attempts:
2 left
💡 Hint
Element-wise operations keep the same size as the input matrices.

Practice

(1/5)
1. In MATLAB, what does the operator .* do when applied between two arrays of the same size?
easy
A. Performs matrix multiplication of the arrays
B. Performs element-wise multiplication of the arrays
C. Divides the first array by the second array element-wise
D. Raises each element of the first array to the power of the second array

Solution

  1. Step 1: Understand the operator .* in MATLAB

    The .* operator multiplies corresponding elements of two arrays of the same size.
  2. Step 2: Differentiate from matrix multiplication

    Matrix multiplication uses * without the dot and follows linear algebra rules, unlike element-wise multiplication.
  3. Final Answer:

    Performs element-wise multiplication of the arrays -> Option B
  4. Quick Check:

    .* means element-wise multiply [OK]
Hint: Dot before operator means element-wise operation [OK]
Common Mistakes:
  • Confusing .* with matrix multiplication *
  • Thinking .* divides arrays
  • Assuming .* raises to power
2. Which of the following is the correct syntax for element-wise division of two arrays A and B in MATLAB?
easy
A. A / B
B. A .\ B
C. A ./ B
D. A // B

Solution

  1. Step 1: Recall element-wise division syntax

    Element-wise division in MATLAB uses ./ between arrays.
  2. Step 2: Identify incorrect options

    / is matrix right division, .\ is element-wise left division, and // is not valid in MATLAB.
  3. Final Answer:

    A ./ B -> Option C
  4. Quick Check:

    ./ means element-wise division [OK]
Hint: Use dot before slash for element-wise division [OK]
Common Mistakes:
  • Using / instead of ./
  • Confusing left and right division operators
  • Using invalid operators like //
3. What is the output of the following MATLAB code?
A = [2, 4; 6, 8];
B = [1, 2; 3, 4];
C = A .^ B;
medium
A. [2, 16; 216, 4096]
B. [2, 8; 18, 32]
C. [2, 4; 6, 8]
D. Error due to incompatible sizes

Solution

  1. Step 1: Understand element-wise power operation

    The operator .^ raises each element of A to the power of the corresponding element in B.
  2. Step 2: Calculate each element

    Calculations:
    2^1 = 2
    4^2 = 16
    6^3 = 216
    8^4 = 4096
  3. Final Answer:

    [2, 16; 216, 4096] -> Option A
  4. Quick Check:

    Element-wise power = calculated matrix [OK]
Hint: Use .^ to power elements individually [OK]
Common Mistakes:
  • Confusing .^ with matrix power ^
  • Calculating powers incorrectly
  • Assuming size mismatch error
4. Consider this MATLAB code snippet:
X = [1, 2, 3];
Y = [4, 5];
Z = X .* Y;

What is the issue with this code?
medium
A. Syntax error due to missing semicolon
B. Code runs correctly and returns [4, 10, 15]
C. Z will be a scalar value
D. Dimension mismatch error for element-wise multiplication

Solution

  1. Step 1: Check array sizes for element-wise multiplication

    X is 1x3 and Y is 1x2, sizes do not match.
  2. Step 2: Understand MATLAB behavior on size mismatch

    Element-wise operations require arrays of the same size or compatible broadcasting, which MATLAB does not support here.
  3. Final Answer:

    Dimension mismatch error for element-wise multiplication -> Option D
  4. Quick Check:

    Different sizes cause error in .* [OK]
Hint: Arrays must be same size for element-wise ops [OK]
Common Mistakes:
  • Assuming MATLAB auto-broadcasts arrays
  • Ignoring size mismatch errors
  • Thinking semicolon affects operation
5. You have two matrices in MATLAB:
M = [1, 2, 3; 4, 5, 6];
N = [2, 0, 1; 3, 4, 5];

You want to create a new matrix R where each element is the result of M element-wise multiplied by N, then divided element-wise by N plus 1. Which MATLAB expression correctly computes R?
hard
A. R = (M .* N) ./ (N + 1);
B. R = M * N ./ (N + 1);
C. R = M .* (N ./ (N + 1));
D. R = (M .* N) / (N + 1);

Solution

  1. Step 1: Identify element-wise multiplication and division

    Element-wise multiplication uses .* and element-wise division uses ./.
  2. Step 2: Analyze each option

    R = (M .* N) ./ (N + 1); correctly uses .* and ./ with parentheses.
    R = M * N ./ (N + 1); uses matrix multiplication *, which is incorrect.
    R = M .* (N ./ (N + 1)); changes the order of operations, not matching the problem.
    R = (M .* N) / (N + 1); uses matrix division /, which is invalid here.
  3. Final Answer:

    R = (M .* N) ./ (N + 1); -> Option A
  4. Quick Check:

    Use dot operators for element-wise multiply and divide [OK]
Hint: Use parentheses and dot operators for element-wise math [OK]
Common Mistakes:
  • Using matrix multiplication instead of element-wise
  • Forgetting dot before division operator
  • Misplacing parentheses changing operation order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard