Bird
Raised Fist0
MATLABdata~10 mins

Vectorization vs loops in MATLAB - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a vector of squares using vectorization.

MATLAB
x = 1:5;
y = x[1]2;
Drag options to blanks, or click blank then click option'
A.^
B^
C*
D.*
Attempts:
3 left
💡 Hint
Common Mistakes
Using ^ instead of .^ causes an error because ^ is matrix power.
Using * or .* does multiplication, not power.
2fill in blank
medium

Complete the code to sum all elements of vector a using a loop.

MATLAB
a = [1, 2, 3, 4, 5];
sum_val = 0;
for i = 1:length(a)
    sum_val = sum_val [1] a(i);
end
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using - subtracts elements instead of adding.
Using * or / does multiplication or division, which is incorrect here.
3fill in blank
hard

Fix the error in the loop that multiplies each element by 2 and stores in b.

MATLAB
a = [1, 2, 3];
b = zeros(size(a));
for i = 1:length(a)
    b(i) = a[1] 2;
end
Drag options to blanks, or click blank then click option'
A.^
B.*
C^
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using .* causes an error because a is scalar inside the loop.
Using ^ or .^ is for power, not multiplication.
4fill in blank
hard

Fill both blanks to create a vector c with elements doubled using vectorization and then sum them.

MATLAB
a = [1, 2, 3, 4];
c = a[1] 2;
total = sum(c[2]);
Drag options to blanks, or click blank then click option'
A.*
B()
C[]
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of .* causes error for vectors.
Using square brackets [] instead of parentheses for function calls.
5fill in blank
hard

Fill all three blanks to create a vector of squares using vectorization, then filter values greater than 10.

MATLAB
x = 1:6;
y = x[1] 2;
z = y(y [2] 10);
result = z[3];
Drag options to blanks, or click blank then click option'
A.^
B>
C'
D.+
Attempts:
3 left
💡 Hint
Common Mistakes
Using ^ instead of .^ causes matrix power error.
Using < or <= instead of > changes the filter condition.
Forgetting to transpose if needed for output shape.

Practice

(1/5)
1. Which of the following best describes vectorization in MATLAB?
easy
A. Writing functions that call themselves recursively
B. Using nested for loops to process each element individually
C. Manually indexing each element to apply operations
D. Performing operations on entire arrays at once without explicit loops

Solution

  1. Step 1: Understand vectorization concept

    Vectorization means applying operations to whole arrays at once, avoiding explicit loops.
  2. Step 2: Compare options

    Only Performing operations on entire arrays at once without explicit loops describes this correctly; others describe loops or recursion.
  3. Final Answer:

    Performing operations on entire arrays at once without explicit loops -> Option D
  4. Quick Check:

    Vectorization = whole-array operations [OK]
Hint: Vectorization means no loops, operate on full arrays [OK]
Common Mistakes:
  • Confusing vectorization with loops
  • Thinking recursion is vectorization
  • Assuming manual indexing is vectorization
2. Which MATLAB syntax correctly sums all elements of a vector A using vectorization?
easy
A. sum = 0; for i = 1:length(A); sum = sum + A(i); end
B. sum = A + A;
C. sum = sum(A);
D. sum = A(1) + A(2);

Solution

  1. Step 1: Identify vectorized sum syntax

    MATLAB's built-in sum() function sums all elements of an array directly.
  2. Step 2: Check options

    sum = sum(A); uses sum(A), which is vectorized. sum = 0; for i = 1:length(A); sum = sum + A(i); end uses a loop. sum = A + A; adds vector to itself. sum = A(1) + A(2); sums only two elements.
  3. Final Answer:

    sum = sum(A); -> Option C
  4. Quick Check:

    sum(A) sums all elements vectorized [OK]
Hint: Use built-in sum() for vectorized addition [OK]
Common Mistakes:
  • Using loops instead of sum()
  • Adding vector to itself instead of summing
  • Summing only part of the vector
3. What is the output of this MATLAB code?
A = [1, 2, 3, 4]; B = A .* 2;
medium
A. [1, 4, 9, 16]
B. [2, 4, 6, 8]
C. [3, 4, 5, 6]
D. Error: Matrix dimensions must agree

Solution

  1. Step 1: Understand element-wise multiplication

    The operator .* multiplies each element of A by 2 individually.
  2. Step 2: Calculate each element

    1*2=2, 2*2=4, 3*2=6, 4*2=8, so B = [2, 4, 6, 8].
  3. Final Answer:

    [2, 4, 6, 8] -> Option B
  4. Quick Check:

    Element-wise multiply by 2 = [2,4,6,8] [OK]
Hint: .* multiplies each element individually [OK]
Common Mistakes:
  • Confusing .* with matrix multiplication (*)
  • Expecting squared values instead of doubled
  • Thinking it causes dimension errors
4. Identify the error in this MATLAB code that tries to double each element of vector A:
A = [1, 2, 3]; for i = 1:length(A); A = A * 2; end
medium
A. The loop variable i is not used inside the loop
B. Using * instead of .* causes dimension mismatch
C. The vector A is not initialized
D. The loop range is incorrect

Solution

  1. Step 1: Analyze the loop body

    The code multiplies entire vector A by 2 each iteration, ignoring i.
  2. Step 2: Understand the effect

    Since i is unused, the loop repeats the same operation, scaling A multiple times (by 8 total) instead of doubling individual elements via indexing.
  3. Final Answer:

    The loop variable i is not used inside the loop -> Option A
  4. Quick Check:

    Loop variable unused causes logic error [OK]
Hint: Use loop variable to index elements inside loop [OK]
Common Mistakes:
  • Ignoring loop variable usage
  • Confusing * and .* operators
  • Assuming vector auto-updates element-wise
5. You want to compute the square of each element in a large vector X efficiently. Which MATLAB code snippet uses vectorization correctly?
hard
A. Y = X .* X;
B. Y = zeros(size(X)); for i = 1:length(X); Y = X(i)^2; end
C. Y = X ^ 2;
D. for i = 1:length(X); Y(i) = X(i)^2; end

Solution

  1. Step 1: Understand vectorized squaring

    Element-wise multiplication .* between X and X squares each element efficiently.
  2. Step 2: Check each option

    for i = 1:length(X); Y(i) = X(i)^2; end uses a loop (less efficient). Y = zeros(size(X)); for i = 1:length(X); Y = X(i)^2; end overwrites Y with a scalar each iteration. Y = X ^ 2; attempts invalid matrix power for vectors. Y = X .* X; is correct.
  3. Final Answer:

    Y = X .* X; -> Option A
  4. Quick Check:

    Element-wise multiply vector by itself = square [OK]
Hint: Use .* to square vectors element-wise [OK]
Common Mistakes:
  • Using ^ instead of .^ or .* for element-wise power
  • Overwriting output inside loop
  • Using loops instead of vectorization
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