Skip to content

MAE Learning

My Automobile Engineering

  • CoursesExpand
    • MBD BasicExpand
      • Introduction to Model Based Development for Automotive Using SIMULINK
      • Introduction to MATLAB Scripting for Automobile Engineering
      • Introduction to Model Based Development for Automotive – Basic Understanding
      • Learn MBD concept focus on MIL and SIL Testing
    • MBD IntermediateExpand
      • Simulink for Automotive with Model-Based Development Basics
      • MATLAB Scripting with Model-Based Development Basics
      • MATLAB Simulink and Scripting Foundations for Automotive MBD
    • MBD AdvanceExpand
      • A Complete Automotive Model Based Development
    • UDS CourseExpand
      • Complete Beginner Program in Automotive MBD and UDS
      • Introduction To Unified Diagnostic Services (UDS) – AUTOSAR and MBD Explained
    • EV CourseExpand
      • Introduction to Battery Technology for Electric Vehicle
    • Free Course
    • Courses Demo Video
  • Study MaterialExpand
    • Automotive EmbeddedExpand
      • Automotive Embedded
      • Software Development
    • MATLABExpand
      • MATLAB Interview Q&A
      • Simulink Tutorial
      • M-Scripting Tutorial
    • AUTOSARExpand
      • AUTOSAR Architecture
    • ISO 26262
  • Blogs
  • MoreExpand
    • About Us
    • Terms and Conditions
    • Contact Us
    • Disclaimer
    • Privacy Policy
Support Login Course Login
MAE Learning
My Automobile Engineering
Support Login Course Login

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 vector
v1 = [1 2 3 4];
% Column vector
v2 = [1; 2; 3; 4];
% Range with step
v3 = 0:0.5:2; % [0 0.5 1.0 1.5 2.0]
% Linearly spaced
v4 = 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 creation
A = [1 2 3; 4 5 6] % 2×3 matrix

Output

A = 2×3

     1     2     3

     4     5     6

% Access elements
val = A(2, 3) % Second row, third column → 6

Output

val = 6

% Modify element
A(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; % integer
b = 3.14; % double
c = '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 ON
torque = 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


🔥 Top Posts
  • Loading...

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 10 other subscribers
  • 56,023 hits
⭐⭐⭐⭐⭐ 4.9/5 Google Reviews
© 2026 MAE Learning. All Rights Reserved.

Categories

Archives

Follow Us

Blog Stats

  • 56,023 hits

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 10 other subscribers
Scroll to top
MAE Learning
WhatsApp
Need Help?
Ask here
  • Courses
    • MBD Basic
      • Introduction to Model Based Development for Automotive Using SIMULINK
      • Introduction to MATLAB Scripting for Automobile Engineering
      • Introduction to Model Based Development for Automotive – Basic Understanding
      • Learn MBD concept focus on MIL and SIL Testing
    • MBD Intermediate
      • Simulink for Automotive with Model-Based Development Basics
      • MATLAB Scripting with Model-Based Development Basics
      • MATLAB Simulink and Scripting Foundations for Automotive MBD
    • MBD Advance
      • A Complete Automotive Model Based Development
    • UDS Course
      • Complete Beginner Program in Automotive MBD and UDS
      • Introduction To Unified Diagnostic Services (UDS) – AUTOSAR and MBD Explained
    • EV Course
      • Introduction to Battery Technology for Electric Vehicle
    • Free Course
    • Courses Demo Video
  • Study Material
    • Automotive Embedded
      • Automotive Embedded
      • Software Development
    • MATLAB
      • MATLAB Interview Q&A
      • Simulink Tutorial
      • M-Scripting Tutorial
    • AUTOSAR
      • AUTOSAR Architecture
    • ISO 26262
  • Blogs
  • More
    • About Us
    • Terms and Conditions
    • Contact Us
    • Disclaimer
    • Privacy Policy
Loading Comments...
Search