0
0
MATLABdata~10 mins

Vectorization vs loops in MATLAB - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Vectorization vs loops
Start
Input Data
Choose Method
Loop
Process
Output
End
This flow shows how data can be processed either by loops or vectorized operations, both leading to the output.
Execution Sample
MATLAB
A = 1:5;
B = zeros(1,5);
for i = 1:5
  B(i) = A(i)^2;
end
This code squares each element of A using a loop and stores results in B.
Execution Table
IterationiA(i)Condition i<=5ActionB after action
111TrueB(1) = 1^2 = 1[1 0 0 0 0]
222TrueB(2) = 2^2 = 4[1 4 0 0 0]
333TrueB(3) = 3^2 = 9[1 4 9 0 0]
444TrueB(4) = 4^2 = 16[1 4 9 16 0]
555TrueB(5) = 5^2 = 25[1 4 9 16 25]
66-FalseExit loop[1 4 9 16 25]
💡 i reaches 6, condition 6<=5 is False, loop ends
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
i-123456
B[0 0 0 0 0][1 0 0 0 0][1 4 0 0 0][1 4 9 0 0][1 4 9 16 0][1 4 9 16 25][1 4 9 16 25]
Key Moments - 3 Insights
Why does the loop stop after i=5?
Because the condition i<=5 becomes false at i=6, as shown in the last row of the execution_table.
Why do we initialize B with zeros before the loop?
To create space for results and avoid errors when assigning values inside the loop, as seen in the variable_tracker start value.
How does vectorization avoid the loop?
Vectorization applies the operation to the whole array at once, skipping the step-by-step iteration shown in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of B after iteration 3?
A[1 4 9 0 0]
B[1 4 0 0 0]
C[1 0 0 0 0]
D[0 0 0 0 0]
💡 Hint
Check the 'B after action' column at iteration 3 in the execution_table.
At which iteration does the loop condition become false?
A5
B6
C4
D1
💡 Hint
Look at the 'Condition i<=5' column in the execution_table and find when it is False.
If we replace the loop with vectorized code B = A.^2, what changes in the execution_table?
AMore iterations because vectorization loops internally
BCondition column will have more True values
CNo iterations, direct assignment of all values at once
DB will remain zeros
💡 Hint
Vectorization applies operation to whole array at once, skipping loop steps shown in execution_table.
Concept Snapshot
Vectorization vs loops in MATLAB:
- Loops process elements one by one.
- Vectorization applies operations to whole arrays at once.
- Vectorized code is usually faster and cleaner.
- Use vectorization when possible for efficiency.
- Loops are easier to understand but slower for large data.
Full Transcript
This lesson compares vectorization and loops in MATLAB. The flow diagram shows data input, choice between loop or vectorized processing, and output. The sample code uses a loop to square elements of an array A and store results in B. The execution table traces each loop iteration: variable i, current element A(i), condition check, action taken, and B's state after. The loop stops when i exceeds 5. The variable tracker shows how i and B change step by step. Key moments clarify why the loop stops, why B is initialized, and how vectorization skips loops. The quiz tests understanding of B's values after iterations, when the loop ends, and how vectorization changes execution. The snapshot summarizes that vectorization applies operations to whole arrays at once, making code faster and cleaner than loops, which process elements one by one.