Vectors in MATLAB
Definition:
Vector means a 1D array of numbers.
It can be of two types:
- Row Vector → Numbers are arranged in a row.
- Column Vector → Numbers are arranged in a column
Examples:
% Row vectorv1 = [1 2 3 4];% Column vectorv2 = [1; 2; 3; 4];% Range with stepv3 = 0:0.5:2; % [0 0.5 1.0 1.5 2.0]% Linearly spacedv4 = linspace(0, 1, 5); % [0 0.25 0.5 0.75 1]
Vector Operations:
So here many operations can be easily performed with these vectors in MATLAB.
1. Addition & Subtraction
In this type of vector operation both vectors should be of the same size.
A = [1 2 3];B = [4 5 6];C = A + B % [5 7 9]D = A - B % [-3 -3 -3]
Output
C = 1×3
5 7 9
D = 1×3
-3 -3 -3



2. Scalar Multiplication
In this scalar multiplication you, all can multiply each element of a vector by a number (scalar).
A = [1 2 3];k = 2;C = k * A % [2 4 6]
Output
C = 1×3
2 4 6


3. Dot Product (Inner Product)
Here we can follow Row × Column method and at the end we will get a single number (scalar) result.
A = [1 2 3];B = [4 5 6];dotProd = dot(A,B) % 1*4 + 2*5 + 3*6 = 32
Output
dotProd = 32

4. Cross Product
So, this basically only applies to 3d vectors and the result is a new vector.
A = [1 2 3];B = [4 5 6];crossProd = cross(A,B) % [-3 6 -3]
Output
crossProd = 1×3
-3 6 -3


5. Element-wise Operations
In element-wise operations it is applied to each element (called .*, ./, .^).
A = [1 2 3];B = [4 5 6];C = A .* B % [4 10 18] (element-wise multiplication)D = A .^ 2 % [1 4 9] (square of each element)
Output
C = 1×3
4 10 18
D = 1×3
1 4 9

Matrices in MATLAB
Definition:
So, a matrix is a 2D array of numbers arranged in rows × columns.
Examples:
% Matrix creationA = [1 2 3; 4 5 6] % 2×3 matrix
Output
A = 2×3
1 2 3
4 5 6

% Access elementsval = A(2, 3) % Second row, third column → 6
Output
val = 6
% Modify elementA(1, 1) = 10
Output
A = 2×3
10 2 3
4 5 6

% Matrix size[r, c] = size(A) % r = 2, c = 3
Output
r = 2
c = 3
Matrix Operations
% Define matrix A (2x3)A = [1 2 3; 4 5 6];% Define matrix B (2x3)B = [1 1 1; 1 1 1];% Matrix addition (both are 2x3)C = A + B;% Matrix multiplication (2x3 * 3x2 → 2x2)D = A * B';% Element-wise multiplication (2x3 .* 2x3 → 2x3)E = A .* B;% Transpose of A (2x3 → 3x2)A_T = A';
Data Types in MATLAB
So, as we know that it is a dynamically typed language, which means that you don’t have to explicitly declare the type of a variable.
a = 10; % integerb = 3.14; % doublec = 'Hello'; % string
Common Data Types

But, in automotive applications (ECU modelling, CAN parsing, signal processing) specific data types are very important:
- int8, uint8, int16, uint16 → for ECU I/O signals (memory optimized).
- single, double → For control algorithms and mathematical models.
- Boolean → ON/OFF or TRUE/FALSE signals (e.g., ignition switch, ABS flag).
- fixed-point (via Fixed-Point Toolbox) → Hardware-level implementation when floating-point is not supported.
Example (CAN Parsing)
speed = uint16(150); % Vehicle speed signal (0–65535 range)ignition = boolean(1); % Ignition ONtorque = int16(-120); % Engine torque (signed value)
These data types ensure that Simulink models and expressions communicate efficiently and efficiently.
Useful MATLAB Functions
v = [1 2 3];M = [1 2; 3 4; 5 6];x = 10;A = [0 1 0];
Commonly Used Functions
length(v) → length of vector (no. of elements).
length(v) % Output: 3
size(M) → indicates the rows and columns of the matrix.
size(M) % Output: 3 2
class(x) → Checks the data type of the variable.
class(x) % Output: ‘double’
isnumeric(x) → Checks whether the variable is numeric or not.
isnumeric(x) % Output: 1 (true)
logical(A) → Converts values to boolean (true/false).
logical(A) % Output: [0 1 0] → false true false
These functions are very useful in basic debugging and data handling, especially when you work with signals and matrices in automotive Simulink/MATLAB.
Vectorization vs Looping
The biggest advantage of MATLAB is that it is optimized for vectorized operations.
Which means you do not need to write a for loop every time. Performing direct operations on a vector/matrix is faster and cleaner.
Example 1: Using Loop
A = [1 2 3 4 5];for i = 1:length(A) B(i) = A(i) ^ 2;end% Output: B = [1 4 9 16 25]
Example 2: Vectorized Way
A = [1 2 3 4 5];B = A .^ 2; % Direct element-wise square% Output: B = [1 4 9 16 25]
Why Vectorization is Better?
- Speed: MATLAB is internally optimized for matrix/vector operations.
- Cleaner Code: The work is done in one line.
- Scalable: Efficient for large data sets and automotive signals.
Automotive Use Cases
