Scatter plots in MATLAB - Time & Space Complexity
When creating scatter plots, we want to know how the time to draw points grows as we add more data.
We ask: How does the program's work increase when the number of points increases?
Analyze the time complexity of the following code snippet.
% x and y are vectors of data points
x = rand(1, n);
y = rand(1, n);
scatter(x, y);
This code creates a scatter plot of n points by plotting each x and y coordinate pair.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Plotting each point on the graph.
- How many times: Once for each of the n points in the data.
As the number of points increases, the time to plot grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 plot operations |
| 100 | 100 plot operations |
| 1000 | 1000 plot operations |
Pattern observation: Doubling the points roughly doubles the work needed to plot them.
Time Complexity: O(n)
This means the time to create the scatter plot grows linearly with the number of points.
[X] Wrong: "Plotting many points takes the same time as plotting just a few points."
[OK] Correct: Each point requires a separate operation, so more points mean more work and more time.
Understanding how plotting time grows helps you explain performance when working with large data sets in real projects.
"What if we used a different plotting function that groups points together? How would the time complexity change?"