Student Notes · 2 Hours · MathWorks Onramp Modules 4–6
name = valueP = V × Ispeed1, speed2…) is impractical. Session 2 solves this with vectors.A vector is an ordered list of numbers stored under a single variable name. Instead of creating ten separate variables for ten speed readings, you create one vector that holds all ten values in sequence.
Numbers side by side, separated by spaces or commas:
v = [0, 20, 40, 60]
This is the most common type for time-series EV data.
Numbers stacked vertically, separated by semicolons:
v = [0; 20; 40; 60]
Used in matrix operations — not needed yet, but good to know.
[1×10], meaning 1 row and 10 columns.There are three main ways to create a vector in MATLAB. Each suits a different situation.
| Syntax | What it produces | EV example |
|---|---|---|
| [a, b, c, ...] | Vector from explicit values | [0, 20, 40, 60, 80] — five speed readings |
| start : end | Sequence from start to end in steps of 1 | t = 0:9 → 0,1,2,3,4,5,6,7,8,9 (10 elements) |
| start : step : end | Sequence with a custom step size | t = 0:0.5:4 → 0, 0.5, 1.0, … 4.0 |
| linspace(a, b, n) | Exactly n values, evenly spaced from a to b | linspace(0, 100, 11) → 0, 10, 20, … 100 |
0:9 produces 10 elements (0 through 9 inclusive). 0:10 produces 11 elements (0 through 10 inclusive). Count carefully — off-by-one is a common mistake, especially when creating time axes.Indexing lets you reach into a vector and pull out exactly the value you need. Think of the index as the address of each element.
speed_kmh = [0, 20, 40, 60, 60, 80, 60, 40, 20, 0]
★ Green = speed_kmh(5) returns 60 | ↑ Orange = max(speed_kmh) = 80 at index 6
v(1), not v(0). Typing v(0) is an error in MATLAB. If you are coming from Python or JavaScript, consciously remind yourself of this — it is the most common indexing mistake.When you apply arithmetic to a vector, MATLAB applies it to every element automatically — no loop needed. This is called an element-wise operation.
for loop to apply an operation to every element of a list. In MATLAB, operations on vectors are element-wise by default — the language was designed for engineers who need to process large data sets in a single, readable expression.| Function | Returns | EV use case |
|---|---|---|
| max(v) | Largest value in v | Peak speed in a drive cycle |
| min(v) | Smallest value in v | Lowest SoC during a trip |
| mean(v) | Average of all values | Average speed — key for range estimation |
| sum(v) | Sum of all values | Total distance (if values are distance increments) |
| length(v) | Number of elements | How many data points in the log |
| find(v < x) | Indices where condition is true | Find all seconds where SoC is below 20% |
A drive cycle is a standardised speed-versus-time profile used to test an EV's range and efficiency under controlled conditions. Every official EV range rating is based on a drive cycle. In MATLAB, a drive cycle is simply a speed vector.
speed_wltp(1:589) for the Low phase. The skills you are learning today are exactly how that is done.Open your Onramp Command Window and complete the following four steps.
Create a time vector t representing 10 seconds (0 through 9) using the colon operator. Then create a speed vector speed_kmh with the values [0, 20, 40, 60, 60, 80, 60, 40, 20, 0] in km/h.
Before moving on, verify: both vectors should have the same number of elements. Use length() to confirm.
Using indexing and built-in functions, find and store three things from speed_kmh:
speed_at_t5peakavgThink about what each of these values tells you about this drive test before moving on.
Convert the entire speed_kmh vector to m/s using a single expression. Store the result as speed_ms. Think about why you can apply the conversion to the entire vector at once rather than element by element.
Work through these before the group discussion. Use Onramp to test your reasoning where useful.
speed_ms(7)? What does that value represent physically in the context of this drive test?speed_kmh by 3.6 work on the whole vector without a loop?v(a:b)speed_kmh(11) on a 10-element vector)length(speed_kmh) to check how many elements exist. Valid indices are 1 through 10.speed_kmh(0) — habit from Python or JavaScriptspeed_kmh(1), not speed_kmh(0).0:10 gives 11 elements, not 100:9 or 0:1:9. Count: 0,1,2,3,4,5,6,7,8,9 = 10 values.length(t) and length(speed_kmh) — both should return 10. Recreate whichever is wrong.Mean() or MAX()mean(), max(), min().: and linspace() create vectors efficiently — no need to type every value
v(1), not v(0)
v(a:b) extracts a sub-vector — essential for isolating time windows in data logs
/ 3.6, * 2, etc.) apply to every value in a vector automatically
max(), mean(), find() work on entire vectors — no loops needed
You now have a drive cycle stored as a vector — 10 speed values over 10 seconds. But looking at a list of numbers like [0, 20, 40, 60, 60, 80, 60, 40, 20, 0] is not the most intuitive way to understand what happened during the drive.
How would you show this data in a way that makes the shape of the journey immediately visible? — Session 3 answers this.