Vectorization vs loops
📖 Scenario: You work as a data analyst and need to process a list of numbers to find their squares. You want to learn how to do this efficiently in MATLAB using both loops and vectorization.
🎯 Goal: Build a MATLAB script that first creates a vector of numbers, then uses a loop to square each number, and finally uses vectorization to do the same task more efficiently. You will compare both methods by printing the results.
📋 What You'll Learn
Create a vector called
numbers with values from 1 to 5Create a zero vector called
squares_loop of the same size as numbersUse a
for loop with variable i to square each element of numbers and store in squares_loopCreate a vector called
squares_vectorized that contains the squares of numbers using vectorized operationPrint both
squares_loop and squares_vectorized to compare💡 Why This Matters
🌍 Real World
Data analysts and engineers often need to process large sets of numbers quickly. Vectorization helps make code faster and cleaner.
💼 Career
Knowing how to replace loops with vectorized operations is a valuable skill in MATLAB programming for scientific computing, data analysis, and engineering tasks.
Progress0 / 4 steps