MATLABTECH

Chapter MATLAB with MATrix

Starting with Array | Vectors | Matrix

At the heart of MATLAB (which stands for MATrix LABoratory) is the matrix. Whether you are processing signals, analyzing data, or building robotic control logic, understanding how to manipulate these data structures is essential.

1. Array Creation

In MATLAB, an array is the most basic data structure. It is simply a collection of data values organized into a grid. You create arrays using square brackets [ ]. Elements can be separated by either spaces or commas.

MATLAB CODE

% Creating a simple array myArray = [10 20 30 40 50]
% Or using commas (produces the exact same result) myArray2 = [10, 20, 30, 40, 50]

2. The Concept of a Vector

A vector is a specific type of one-dimensional array. It can be oriented horizontally (a row vector) or vertically (a column vector).

  • Row Vectors: Created by separating elements with spaces or commas.

  • Column Vectors: Created by separating elements with semicolons ;.

MATLAB CODE

% This is a 1×4 Row Vector
rowVec = [1, 2, 3, 4]

% This is a 4×1 Column Vector
colVec = [1; 2; 3; 4]

3. The Concept of a Matrix

A matrix is a two-dimensional array consisting of rows and columns. Think of it as stacking multiple row vectors on top of each other. You use spaces (or commas) to separate columns, and semicolons ; to move to the next row.

MATLAB CODE

% Creating a 3×3 Matrix
% Row 1: 1 2 3 | Row 2: 4 5 6 | Row 3: 7 8 9
A = [1 2 3; 4 5 6; 7 8 9]

4. Matrix Operations & Arithmetic

MATLAB is heavily optimized for matrix math. Here are the core operations you will use daily.

To extract a specific value from a matrix, you use parentheses () and specify the (row, column) coordinates.

MATLAB CODE

M = [10 20 30; 40 50 60; 70 80 90];

% Extract the value in the 2nd row, 3rd column (which is 60)
val = M(2, 3)

% Extract the entire 1st row (using the colon operator)
firstRow = M(1, 🙂

Addition and Subtraction

Matrices must be the exact same size to be added or subtracted. The operation is performed element-by-element.

MATLAB CODE

X = [1 2; 3 4];
Y = [5 6; 7 8];

% Matrix Addition
sumMat = X + Y % Result: [6 8; 10 12]

% Matrix Subtraction
diffMat = Y – X % Result: [4 4; 4 4]

Matrix Multiplication

MATLAB handles two distinct types of multiplication. This is a crucial concept to master:

  1. Standard Matrix Multiplication (*): Follows the rules of linear algebra (dot products of rows and columns). The number of columns in the first matrix must match the number of rows in the second.

  2. Element-wise Multiplication (.*): Multiplies corresponding elements together. Matrices must be the exact same size. Notice the period . before the asterisk.

MATLAB CODE

A = [1 2; 3 4];
B = [2 0; 1 2];

% Standard Linear Algebra Multiplication
result1 = A * B

% Element-by-Element Multiplication
result2 = A .* B

Matrix Inversion

In linear algebra, dividing by a matrix isn’t technically possible. Instead, you multiply by its inverse. You can find the inverse of a square matrix using the inv() function.

MATLAB CODE

C = [4 3; 3 2];

% Calculate the inverse
invC = inv(C)

% Verifying the inverse (should return the Identity Matrix)
C * invC