MATLAB Onramp Progress after Session 2 Modules 1–6 of 12  (50%)
🔁

Session 1 Recap

Variables
Named memory containers created with name = value
Arithmetic
MATLAB evaluates expressions — e.g. P = V × I
Workspace
Tracks every variable currently stored — your live data view
The Problem
In Session 1 every variable held a single value. But real EV data is never a single number — it is thousands of time-stamped readings. A 10-second drive test produces 10 speed values. A full WLTP cycle has 1800 data points. Storing each one in a separate variable (speed1, speed2…) is impractical. Session 2 solves this with vectors.
📊

What is a Vector?

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.

Diagram — scalar vs vector
flowchart LR subgraph S1["Session 1 approach — one variable per value"] direction LR A["speed1 = 0"]:::old B["speed2 = 20"]:::old C["speed3 = 40"]:::old D["... 7 more ..."]:::dots end subgraph S2["Session 2 approach — one vector for all values"] direction LR E["speed_kmh = [0, 20, 40, 60, 60, 80, 60, 40, 20, 0]"]:::good end S1 -->|"replaced by"| S2 classDef old fill:#FEE2E2,stroke:#DC2626,color:#991B1B classDef dots fill:#F1F5F9,stroke:#94A3B8,color:#94A3B8,font-style:italic classDef good fill:#D1FAE5,stroke:#059669,color:#065F46,font-weight:bold

Row Vector

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.

Column Vector

Numbers stacked vertically, separated by semicolons:

v = [0; 20; 40; 60]

Used in matrix operations — not needed yet, but good to know.

Key Idea
A vector is still one variable. One name — many values. The Workspace panel will show it as a single entry with a size like [1×10], meaning 1 row and 10 columns.

Useful size functions

v = [0, 20, 40, 60, 80]; length(v) % → 5 (number of elements) size(v) % → [1, 5] (rows, columns) numel(v) % → 5 (total element count)
🔨

Creating Vectors

There are three main ways to create a vector in MATLAB. Each suits a different situation.

Diagram — three ways to create a vector
flowchart TD Q["I need to create a vector"]:::q Q --> A["I know every value\nexplicitly"] Q --> B["I want a sequence\nwith a fixed step"] Q --> C["I want N evenly\nspaced values\nbetween two limits"] A --> A1["Bracket notation\n[0, 20, 40, 60]"]:::method B --> B1["Colon operator\nstart : step : end"]:::method C --> C1["linspace()\nlinspace(a, b, n)"]:::method A1 --> A2["speed = [0,20,40,60]"]:::example B1 --> B2["t = 0:1:9"]:::example C1 --> C2["temp = linspace(25,45,5)"]:::example classDef q fill:#F1F5F9,stroke:#94A3B8,color:#1A2B3C,font-weight:bold classDef method fill:#E0F2FE,stroke:#0284C7,color:#0369A1,font-weight:bold classDef example fill:#D1FAE5,stroke:#059669,color:#065F46,font-style:italic

Syntax reference

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
Count Check
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.
% Three ways — try all of these in Onramp t = 0:1:9 % → [0 1 2 3 4 5 6 7 8 9] (10 elements) speed = 0:10:90 % → [0 10 20 ... 90] (10 elements) temps = linspace(25, 45, 5) % → [25 30 35 40 45] (5 elements) length(t) % should return 10
🎯

Indexing — Accessing Specific Values

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]

value
0
20
40
60
60
80
60
40
20
0
index
1
2
3
4
5
6
7
8
9
10

★ Green = speed_kmh(5) returns 60  |  ↑ Orange = max(speed_kmh) = 80 at index 6

Diagram — indexing patterns
flowchart LR subgraph Single["Single element"] A1["v(n)"]:::syn --> A2["value at position n"]:::res A3["v(end)"]:::syn --> A4["last element"]:::res A5["v(end-1)"]:::syn --> A6["second to last"]:::res end subgraph Range["Range / Slice"] B1["v(a:b)"]:::syn --> B2["elements a through b\ninclusive"]:::res B3["v(end-2:end)"]:::syn --> B4["last three elements"]:::res end classDef syn fill:#E0F2FE,stroke:#0284C7,color:#0369A1,font-weight:bold,font-family:monospace classDef res fill:#D1FAE5,stroke:#059669,color:#065F46
speed_kmh = [0, 20, 40, 60, 60, 80, 60, 40, 20, 0]; speed_kmh(5) % → 60 (value at index 5) speed_kmh(end) % → 0 (last element) speed_kmh(end-1) % → 20 (second to last) speed_kmh(2:5) % → [20, 40, 60, 60] (slice: index 2 to 5) speed_kmh(end-2:end) % → [40, 20, 0] (last three elements)
1-Based Indexing
MATLAB counts from 1, not 0. The first element is always 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.
⚙️

Element-Wise Operations

When you apply arithmetic to a vector, MATLAB applies it to every element automatically — no loop needed. This is called an element-wise operation.

Diagram — element-wise operation: dividing a vector by a scalar
flowchart LR subgraph IN["speed_kmh (km/h)"] direction LR A["0"]:::v B["20"]:::v C["40"]:::v D["60"]:::v E["80"]:::v end OP["÷ 3.6\n(applied to each element)"]:::op subgraph OUT["speed_ms (m/s)"] direction LR F["0"]:::r G["5.56"]:::r H["11.11"]:::r I["16.67"]:::r J["22.22"]:::r end IN --> OP --> OUT classDef v fill:#E0F2FE,stroke:#0284C7,color:#0369A1,font-weight:bold classDef op fill:#FEF3C7,stroke:#D97706,color:#92400E,font-weight:bold,font-size:14px classDef r fill:#CCFBF1,stroke:#0D9488,color:#115E59,font-weight:bold
speed_kmh = [0, 20, 40, 60, 80]; % Convert entire vector from km/h to m/s in one line speed_ms = speed_kmh / 3.6 % → [0, 5.56, 11.11, 16.67, 22.22] % Other element-wise examples speed_kmh * 2 % doubles every speed value speed_kmh + 10 % adds 10 km/h to every element speed_kmh .^ 2 % squares every element (.^ for element-wise power)
Why No Loop?
In most programming languages you would need a 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.

Useful built-in vector functions

FunctionReturnsEV use case
max(v)Largest value in vPeak speed in a drive cycle
min(v)Smallest value in vLowest SoC during a trip
mean(v)Average of all valuesAverage speed — key for range estimation
sum(v)Sum of all valuesTotal distance (if values are distance increments)
length(v)Number of elementsHow many data points in the log
find(v < x)Indices where condition is trueFind all seconds where SoC is below 20%

EV Context — Drive Cycles

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.

Diagram — real-world drive cycles as MATLAB vectors
flowchart TD ROOT["Drive Cycle Data"]:::root ROOT --> A["WLTP\n(Europe / India)\n1800 seconds\n4 phases: Low → Extra-High"]:::cycle ROOT --> B["MIDC\n(India)\n1180 seconds\nUrban driving pattern"]:::cycle ROOT --> C["Today's simplified\ncycle\n10 seconds, 10 points"]:::cycle A --> A1["t = 0:1:1799\nspeed = [...] (1800 values)"]:::code B --> B1["t = 0:1:1179\nspeed = [...] (1180 values)"]:::code C --> C1["t = 0:1:9\nspeed = [0,20,40,60,60,80,...]"]:::code classDef root fill:#F1F5F9,stroke:#94A3B8,color:#1A2B3C,font-weight:bold classDef cycle fill:#D1FAE5,stroke:#059669,color:#065F46,font-weight:bold classDef code fill:#E0F2FE,stroke:#0284C7,color:#0369A1,font-family:monospace

What you can do with a drive cycle vector

speed_kmh = [0,20,40,60,60,80,60,40,20,0]; max(speed_kmh) % peak speed mean(speed_kmh) % average speed — key range metric speed_kmh(1:5) % first half of the cycle speed_ms = speed_kmh / 3.6 % convert to m/s for physics calcs
Did You Know
The WLTP cycle has four speed phases — Low, Medium, High, and Extra-High. Engineers analyse each phase separately using vector slicing, e.g. speed_wltp(1:589) for the Low phase. The skills you are learning today are exactly how that is done.
Units
Drive cycle speed data is typically given in km/h. Physics calculations (force, energy, aerodynamic drag) need m/s. The conversion is: m/s = km/h ÷ 3.6. With element-wise operations you convert an entire cycle vector in a single line.
🔧

Block D — EV Task

Block D ⏱ 15 min Drive Cycle Vector & Unit Conversion

Open your Onramp Command Window and complete the following four steps.

1
Create the drive cycle vectors

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.

2
Index and explore the vector

Using indexing and built-in functions, find and store three things from speed_kmh:

  • The speed at second 5 — store it as speed_at_t5
  • The peak (maximum) speed — store it as peak
  • The average speed — store it as avg

Think about what each of these values tells you about this drive test before moving on.

3
Convert units

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.

4
Reflect on these questions

Work through these before the group discussion. Use Onramp to test your reasoning where useful.

Q1 — What is speed_ms(7)? What does that value represent physically in the context of this drive test?
Hint: think about what index 7 corresponds to in the time vector
Q2 — Why does dividing speed_kmh by 3.6 work on the whole vector without a loop?
Hint: this is a property of how MATLAB handles arithmetic on vectors
Q3 — What does the shape of this drive cycle tell you about the vehicle's behaviour? Where is it accelerating, cruising, and braking?
Hint: look at where speed increases, stays constant, and decreases
Q4 — How would you extract only the speeds from the second half of the cycle (seconds 6 through 10)? Write the indexing expression.
Hint: use range indexing v(a:b)
Note
The steps above give you the structure — the expressions and the reasoning are yours to work through. If you are stuck on a step, revisit the relevant section above before asking for help.

Troubleshooting — if something goes wrong

❌ Index exceeds vector dimensions
Cause: You used an index larger than the vector length (e.g. speed_kmh(11) on a 10-element vector)
Fix: Use length(speed_kmh) to check how many elements exist. Valid indices are 1 through 10.
❌ Index with 0 error
Cause: Trying speed_kmh(0) — habit from Python or JavaScript
Fix: MATLAB is 1-based. The first element is speed_kmh(1), not speed_kmh(0).
⚠️ Wrong element count from colon operator
Cause: 0:10 gives 11 elements, not 10
Fix: For 10 elements (0 through 9), use 0:9 or 0:1:9. Count: 0,1,2,3,4,5,6,7,8,9 = 10 values.
⚠️ t and speed_kmh have different lengths
Cause: Accidentally created vectors of different sizes
Fix: Use length(t) and length(speed_kmh) — both should return 10. Recreate whichever is wrong.
🔍 mean() or max() not working
Cause: Function name capitalised — Mean() or MAX()
Fix: MATLAB function names are lowercase: mean(), max(), min().

Key Takeaways

📊 A vector is an ordered list of values stored under a single variable name
➡️ The colon operator : and linspace() create vectors efficiently — no need to type every value
1️⃣ MATLAB uses 1-based indexing — the first element is v(1), not v(0)
✂️ Slicing with v(a:b) extracts a sub-vector — essential for isolating time windows in data logs
⚙️ Element-wise operations (/ 3.6, * 2, etc.) apply to every value in a vector automatically
Drive cycles like WLTP and MIDC are speed vectors — the same MATLAB structure as today's task, just longer
📈 Built-in functions max(), mean(), find() work on entire vectors — no loops needed

Before Session 3 — think about this

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.

Session 3 →