Vectorization helps you do many calculations at once, making your code faster and simpler. Loops do one step at a time, which can be slower.
Vectorization vs loops in MATLAB
Loop example: for i = 1:length(A) B(i) = A(i) * 2; end Vectorized example: B = A .* 2;
Loops repeat commands for each item one by one.
Vectorization applies operations to whole arrays at once.
A = [1, 2, 3, 4]; B = zeros(size(A)); for i = 1:length(A) B(i) = A(i)^2; end
A = [1, 2, 3, 4]; B = A.^2;
A = [1, 2, 3, 4]; B = zeros(size(A)); for i = 1:length(A) if A(i) > 2 B(i) = A(i) * 10; else B(i) = A(i); end end
A = [1, 2, 3, 4]; B = A; B(A > 2) = A(A > 2) * 10;
This program shows how to double numbers in a list using a loop and vectorization. Both give the same result, but vectorization is simpler.
A = 1:5; % Using loop to double each element B_loop = zeros(size(A)); for i = 1:length(A) B_loop(i) = A(i) * 2; end % Using vectorization to double each element B_vector = A .* 2; % Display results fprintf('Using loop: '); fprintf('%d ', B_loop); fprintf('\nUsing vectorization: '); fprintf('%d ', B_vector); fprintf('\n');
Vectorized code usually runs faster in MATLAB because it uses optimized math operations.
Loops are easier to understand for beginners but can be slower for big data.
Use vectorization when possible, but loops are fine for complex steps that can't be vectorized.
Vectorization applies operations to whole arrays at once, making code faster and shorter.
Loops repeat commands step-by-step and can be slower but are sometimes necessary.
In MATLAB, prefer vectorization for better performance and cleaner code.
Practice
Solution
Step 1: Understand vectorization concept
Vectorization means applying operations to whole arrays at once, avoiding explicit loops.Step 2: Compare options
Only Performing operations on entire arrays at once without explicit loops describes this correctly; others describe loops or recursion.Final Answer:
Performing operations on entire arrays at once without explicit loops -> Option DQuick Check:
Vectorization = whole-array operations [OK]
- Confusing vectorization with loops
- Thinking recursion is vectorization
- Assuming manual indexing is vectorization
A using vectorization?Solution
Step 1: Identify vectorized sum syntax
MATLAB's built-insum()function sums all elements of an array directly.Step 2: Check options
sum = sum(A);usessum(A), which is vectorized.sum = 0; for i = 1:length(A); sum = sum + A(i); enduses a loop.sum = A + A;adds vector to itself.sum = A(1) + A(2);sums only two elements.Final Answer:
sum = sum(A); -> Option CQuick Check:
sum(A) sums all elements vectorized [OK]
- Using loops instead of sum()
- Adding vector to itself instead of summing
- Summing only part of the vector
A = [1, 2, 3, 4]; B = A .* 2;
Solution
Step 1: Understand element-wise multiplication
The operator.*multiplies each element ofAby 2 individually.Step 2: Calculate each element
1*2=2, 2*2=4, 3*2=6, 4*2=8, soB = [2, 4, 6, 8].Final Answer:
[2, 4, 6, 8] -> Option BQuick Check:
Element-wise multiply by 2 = [2,4,6,8] [OK]
- Confusing .* with matrix multiplication (*)
- Expecting squared values instead of doubled
- Thinking it causes dimension errors
A:A = [1, 2, 3]; for i = 1:length(A); A = A * 2; end
Solution
Step 1: Analyze the loop body
The code multiplies entire vectorAby 2 each iteration, ignoringi.Step 2: Understand the effect
Sinceiis unused, the loop repeats the same operation, scalingAmultiple times (by 8 total) instead of doubling individual elements via indexing.Final Answer:
The loop variableiis not used inside the loop -> Option AQuick Check:
Loop variable unused causes logic error [OK]
- Ignoring loop variable usage
- Confusing * and .* operators
- Assuming vector auto-updates element-wise
X efficiently. Which MATLAB code snippet uses vectorization correctly?Solution
Step 1: Understand vectorized squaring
Element-wise multiplication.*betweenXandXsquares each element efficiently.Step 2: Check each option
for i = 1:length(X); Y(i) = X(i)^2; enduses a loop (less efficient).Y = zeros(size(X)); for i = 1:length(X); Y = X(i)^2; endoverwritesYwith a scalar each iteration.Y = X ^ 2;attempts invalid matrix power for vectors.Y = X .* X;is correct.Final Answer:
Y = X .* X; -> Option AQuick Check:
Element-wise multiply vector by itself = square [OK]
- Using ^ instead of .^ or .* for element-wise power
- Overwriting output inside loop
- Using loops instead of vectorization
Ready to prove it?
Pass the test and this pattern moves unknown
