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?"
Practice
Solution
Step 1: Understand scatter plot purpose
A scatter plot shows individual points for pairs of values, usually X and Y coordinates.Step 2: Compare options to definition
Only Points representing pairs of values on an X-Y graph describes points on an X-Y graph, which matches scatter plot behavior.Final Answer:
Points representing pairs of values on an X-Y graph -> Option CQuick Check:
Scatter plot = points on X-Y graph [OK]
- Confusing scatter plot with line plot
- Thinking scatter plots show bars or pie slices
- Assuming scatter plots connect points
x and y?Solution
Step 1: Recall MATLAB scatter syntax
The correct function to create scatter plots isscatter(x, y).Step 2: Check other options for syntax errors
Options A, C, and D are not valid MATLAB commands for scatter plots.Final Answer:
scatter(x, y) -> Option BQuick Check:
scatter function syntax = scatter(x, y) [OK]
- Using dot notation like plot.scatter
- Typing scatterplot instead of scatter
- Omitting parentheses or commas
x = 1:5;
y = [2 4 6 8 10];
scatter(x, y, 100, 'r', 'filled')
Solution
Step 1: Understand scatter parameters
The code usesscatter(x, y, 100, 'r', 'filled')which sets point size to 100, color red ('r'), and fills the markers.Step 2: Interpret the plot output
The plot shows red filled circles at points (1,2), (2,4), (3,6), (4,8), and (5,10).Final Answer:
A scatter plot with red filled circles sized 100 at points (1,2), (2,4), ..., (5,10) -> Option AQuick Check:
scatter(x,y,100,'r','filled') = red filled circles [OK]
- Thinking 'filled' causes error
- Confusing scatter with line plot
- Ignoring size and color parameters
x = 1:4;
y = [5 6 7];
scatter(x, y)
Solution
Step 1: Check vector lengths
Vectorxhas length 4, butyhas length 3, which is invalid for scatter.Step 2: Understand scatter input requirements
Scatter requiresxandyto be the same length to plot pairs of points.Final Answer:
Vectors x and y have different lengths causing an error -> Option AQuick Check:
Length mismatch in x,y vectors = error [OK]
- Assuming scatter auto-fixes length mismatch
- Thinking size argument is mandatory
- Believing scatter can't plot numeric data
x1 = [1 2 3]; y1 = [4 5 6];
x2 = [1 2 3]; y2 = [7 8 9];
How can you plot both groups on the same scatter plot with different colors using filled markers and add a legend?
Solution
Step 1: Plot first group with filled blue markers
Usescatter(x1, y1, 'b', 'filled')to plot blue filled points.Step 2: Use
To overlay plots,hold onbefore plotting second grouphold onkeeps the first plot visible before adding the second.Step 3: Plot second group with filled red markers and add legend
Plot second group withscatter(x2, y2, 'r', 'filled')and add legend for clarity.Final Answer:
scatter(x1, y1, 'b', 'filled'); hold on; scatter(x2, y2, 'r', 'filled'); legend('Group 1', 'Group 2') -> Option DQuick Check:
Use hold on to overlay scatter plots with legend [OK]
- Forgetting hold on causes second plot to overwrite first
- Using plot instead of scatter for points
- Omitting legend or using incorrect syntax
Ready to prove it?
Pass the test and this pattern moves unknown
