Learn Python, Microsoft 365 and Google Workspace
Connect with me: Youtube | LinkedIn | WhatsApp Channel | Web | Facebook | Twitter
To access the updated handouts, please click on the following link: https://yasirbhutta.github.io/matlab/docs/vectors-matrices.html
Important: To perform matrix multiplication, the first matrix must have the same number of columns as the second matrix has rows. The number of rows of the resulting matrix equals the number of rows of the first matrix, and the number of columns of the resulting matrix equals the number of columns of the second matrix. [1]
Important: In MATLAB, the concept of conjugate refers to the complex conjugate of a number or element within a matrix. The complex conjugate of a complex number is a new number with the same real part but the imaginary part negated.
Vectors are one-dimensional arrays used to store a collection of numbers. You can create them in different ways:
[]
:
myVector = [1 2 3 4 5];
:
:
rangeVector = 1:5; % Creates a vector from 1 to 5 (inclusive)
There are two main ways to access individual elements within a vector:
Using Indices:
myVector(2) % Accesses the second element (value 2)
rangeVector(4) % Accesses the fourth element (value 4)
Using Colon Operator :
:
vector(start:end)
: Accesses elements from start
(inclusive) to end
(inclusive).vector(start:step:end)
: Accesses elements with a specific step
between them (similar to Python slicing).vector(:)
: Accesses all elements of the vector.
myVector(2:4) % Accesses elements from index 2 (value 2) to index 4 (value 4)
rangeVector(1:2:5) % Accesses elements 1, 3, and 5 (with a step of 2)
myVector(:) % Accesses all elements (same as `myVector`)
fruits = ["apple" "banana" "orange"]; % Create a vector of strings
secondFruit = fruits(2); % Access the second element ("banana")
temperatures = [20 32 25 18];
firstTwo = temperatures(1:2); % Extract the first two elements ([20 32])
evenIndices = temperatures(2:2:end); % Extract elements at even indices ([32 18])
Tips
end
as an index to refer to the last element: myVector(end)
.See also:
Linspace in MATLAB is a function used to generate a row vector containing evenly spaced values between two specified endpoints.
Syntax:
y = linspace(x1, x2)
: This creates a row vector with 100 (default) points between x1
and x2
.y = linspace(x1, x2, n)
: This allows you to specify the number of points (n
) in the vector.Key Points:
(x2 - x1) / (n - 1)
.x1
) and ending (x2
) points in the output vector.:
) but offers more control over the number of points.Use Cases:
See also:
In MATLAB, the logspace
function is used to generate a row vector containing logarithmically spaced values between two specified points. It’s the logarithmic counterpart of the linspace
function you saw earlier.
Syntax:
y = logspace(a, b)
: This creates a row vector with 50 points (default) between decades of 10^a and 10^b.y = logspace(a, b, n)
: This allows you to specify the number of points (n
) in the vector.y = logspace(a, pi)
: This is useful for creating logarithmically spaced frequencies (especially in digital signal processing) in the interval [10^a, pi].Key Points:
It includes an approximation of the starting point (10^a) and the ending point (might not be exactly pi if used) in the output vector.
See also
v = [1, 2, 3, 4, 5];
total_sum = sum(v);
disp(total_sum) % Output: 15
```matlab
### 2. mean(x)
```matlab
v = [4, 6, 7, 3, 5];
average_value = mean(v);
disp(average_value) % Output: 5
v = [1, 2, 3, 4, 5];
vector_length = length(v);
disp(vector_length) % Output: 5
Example #: Finding the maximum element
v = [1, 5, 2, 8, 3];
max_element = max(v);
disp(max_element) % Output: 8
Example #: Finding the minimum element
v = [5, 2, 8, 1, 3];
minimum_value = min(v);
disp(minimum_value) % Output: 1
Example #: Product of all elements
v = [2, 3, 4, 1];
total_product = prod(v);
disp(total_product) % Output: 24
Example #: Finding the sign of each element:
v = [1, -2, 0, 3.5, -4];
element_signs = sign(v);
disp(element_signs) % Output: 1 -1 0 1 -1
This code creates a vector v and uses sign(v) to determine the sign of each element. The results are stored in element_signs. The output shows 1 for positive values, -1 for negative values, and 0 for zero.
find
function in MATLAB is a versatile tool for finding specific elements or their indices within a vector.1. Finding Non-Zero Elements:
k = find(v)
. This returns a vector k
containing the linear indices of all non-zero elements in vector v
.Example:
v = [1, 0, 3, 0, 5];
k = find(v);
disp(k) % Output: 1 3 5
2. Finding a Specific Number of Elements:
k = find(v, n)
returns the first n
indices of non-zero elements.k = find(v, n, 'last')
returns the last n
indices of non-zero elements.Example:
v = [2, 0, -1, 4, 0];
first_two = find(v, 2); % Find the first two non-zero elements
last_two = find(v, 2, 'last'); % Find the last two non-zero elements
disp(first_two) % Output: 1 3
disp(last_two) % Output: 3 4
3. Finding Elements Based on Conditions:
<
, >
, <=
, >=
, ==
, ~=
) within find
to locate elements meeting specific criteria.Example:
v = [8, 2, 6, 1, 9];
greater_than_5 = find(v > 5); % Find indices of elements greater than 5
equal_to_2 = find(v == 2); % Find indices of elements equal to 2
disp(greater_than_5) % Output: 1 4 5
disp(equal_to_2) % Output: 2
Remember, the find
function is a powerful tool for manipulating vectors in MATLAB. Explore the documentation https://www.mathworks.com/help/matlab/ref/find.html for more details and advanced usage scenarios.
Example #: Fixing all elements in a vector
v = [-2.5 1.8 4.3 -7.1];
fixed_v = fix(v);
disp(fixed_v) % Output: -2 1 4 -7
This code creates a vector v with decimal values. fix(v) applies the fix function to each element, rounding them towards zero and storing the result in fixed_v.
The floor function in MATLAB is useful for rounding down elements in vectors (and arrays) to the nearest integer less than or equal to that element.
Important: floor always rounds down towards negative infinity, regardless of the sign of the input value.
Example #: Rounding down elements in a vector
v = [1.5, 2.8, 3.1, 4.9, 5.2];
floored_vector = floor(v);
disp(floored_vector) % Output: 1 2 3 4 5
Example #: Rounding down negative decimals
v = [-1.5, 2.8, -3.1, 4.9, -5.2];
floored_vector = floor(v);
disp(floored_vector) % Output: -2 2 -4 4 -6
Important point: ceil always rounds up towards positive infinity.
Example #: Rounding towards positive infinity
v = [-1.5, 2.8, -3.1, 4.9, -5.2];
ceiling_vector = ceil(v);
disp(ceiling_vector) % Output: -1 3 -3 5 -5
In this example, ceil(v) rounds each element in v towards positive infinity. So:
Example #: Rounding to nearest integers
v = [1.5, 2.8, -3.7, 4.9, -5.2];
rounded_vector = round(v);
disp(rounded_vector) % Output: 2 3 -4 5 -5
Example #: Sorting in ascending order (default)
v = [5, 1, 4, 2, 3];
sorted_vector = sort(v);
disp(sorted_vector) % Output: 1 2 3 4 5
This code creates a vector v and uses sort(v) to sort its elements in ascending order. The result is stored in sorted_vector.
Example #: Sorting in descending order You can specify ‘descend’ as the second argument to sort in descending order:
sorted_descending = sort(v, 'descend');
disp(sorted_descending) % Output: 5 4 3 2 1
The mod
function in MATLAB is useful for finding the remainder after element-wise division of vectors. Here are some examples of how to use it with vectors:
1. Modulo with a Scalar:
Example:
v = [10, 5, 18, 3, 12];
mod_result = mod(v, 4); % Find remainder after dividing each element by 4
disp(mod_result) % Output: 2 1 2 3 0
2. Modulo Between Vectors:
Example:
v1 = [7, 11, 15];
v2 = [3, 4, 5];
mod_result = mod(v1, v2);
disp(mod_result) % Output: 1 3 0 (Remainder of v1(i) / v2(i))
Key points:
mod
function follows the convention that mod(a,0) returns a
. In other words, the remainder when dividing by zero is the original dividend itself.mod
is the same as the inputs.By understanding these examples, you can effectively use the mod
function to perform modulo operations on vectors in MATLAB.
The rem
function in MATLAB behaves very similarly to mod
for vectors, but with a subtle difference. Here’s how to use it with vectors:
1. Rem with a Scalar:
Example:
v = [10, 5, 18, 3, 12];
rem_result = rem(v, 4); % Find remainder after dividing each element by 4
disp(rem_result) % Output: 2 1 2 3 0
2. Rem Between Vectors:
Example:
v1 = [7, 11, 15];
v2 = [3, 4, 5];
rem_result = rem(v1, v2);
disp(rem_result) % Output: 1 3 0 (Remainder of v1(i) / v2(i))
3. Key Difference from mod:
rem
and mod
lies in the handling of negative dividends.
mod
always returns a non-negative remainder between 0 and the divisor minus one.rem
however, signs the remainder according to the sign of the dividend.Example:
v = [-10, 5, -18, 3, -12];
rem_result = rem(v, 4); % Remainder considering sign of dividend
disp(rem_result) % Output: -2 1 -2 3 -0
Example #: Creating a 2x2 Matrix
A = [1 2; 3 4];
disp(A);
This creates a 2x2 matrix A
with elements:
1 2
3 4
Example #: Creating a 3x3 Matrix
% Create a 3x3 matrix
matrix = [1 2 3; 4 5 6; 7 8 9]
Example #: Create a 2x3 matrix
C = [1 2 3; 4 5 6]
Example #: Specifying elements with spaces or commas
Elements within a row can be separated by either commas ( , ) or spaces. Both notations achieve the same result.
% Using commas
matrix_comma = [1, 4, 7; 2, 5, 8; 3, 6, 9]
% Using spaces
matrix_space = [1 4 7; 2 5 8; 3 6 9]
Example #: Creating a 4x2 Matrix
C = [2, 4; 6, 8; 10, 12; 14, 16];
This creates a 4x2 matrix C
with elements:
2 4
6 8
10 12
14 16
Example #: Creating a 3x4 Matrix
D = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12];
This creates a 3x4 matrix D
with elements:
1 2 3 4
5 6 7 8
9 10 11 12
Example #: Creating a 1x5 Row Vector
E = [1, 2, 3, 4, 5];
This creates a 1x5 row vector E
with elements:
1 2 3 4 5
Example #: Creating a 5x1 Column Vector:
F = [6; 7; 8; 9; 10];
This creates a 5x1 column vector F
with elements:
6
7
8
9
10
See also:
In MATLAB, indexing into matrices allows you to access and manipulate specific elements, rows, columns, or subsets of a matrix. Here are some examples of matrix indexing in MATLAB:
Example #: Accessing single elements:
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
element = A(2, 3); % Accesses the element in the second row and third column
disp(element); % Displays: 6
Example #: Accessing entire rows or columns:
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
row = A(2, :); % Accesses the entire second row
disp(row); % Displays: 4 5 6
column = A(:, 3); % Accesses the entire third column
disp(column); % Displays: 3 6 9
Example #: Assigning values to specific elements:
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
A(1, 2) = 10; % Assigns the value 10 to the element in the first row and second column
disp(A); % Displays the updated matrix
% Resulting matrix:
% 1 10 3
% 4 5 6
% 7 8 9
Example #: Indexing with linear indices:
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
linear_index = [1, 4, 7]; % Linear indices of elements to access
elements = A(linear_index); % Accesses elements using linear indices
disp(elements); % Displays elements corresponding to linear indices
The Output of the code is:
1 2 3
Example #: Indexing with logical arrays:
A = [1, 2, 3; 10, 5, 1; 4, 8, 9];
logical_index = A > 5; % Creates a logical array indicating elements greater than 5
disp(logical_index); % Displays the logical array
% Resulting logical array:
% 0 0 0
% 1 0 0
% 0 1 1
% Using logical array to access elements
elements_gt_5 = A(logical_index);
disp(elements_gt_5); % Displays elements greater than 5
% Output
% 10
% 8
% 9
Using square brackets ([]):
Example #: Horizontal concatenation:
A = [1 2; 3 4];
B = [5 6; 7 8];
% Using square brackets
C = [A, B];
disp(C);
Example #: Vertical concatenation
A = [1 2; 3 4];
B = [5 6; 7 8; 9 10];
% Using square brackets
C = [A; B];
disp(C);
Example #
% Creating two matrices
A = [1 2 3; 4 5 6];
B = [7 8 9; 10 11 12];
% Vertical concatenation
C = [A; B];
disp(C);
Output:
1 2 3
4 5 6
7 8 9
10 11 12
Example #: Mixed Concatenation (Combination of Vertical and Horizontal)
% Creating matrices
A = [1 2; 3 4];
B = [5 6; 7 8];
C = [9 10 11 12];
% Horizontal concatenation of A and B, followed by vertical concatenation with C
D = [A B; C];
disp(D);
Output:
1 2 5 6
3 4 7 8
9 10 11 12
Class Activity
Creating Matrices with Different Sizes:
disp(D)
, disp(E)
, and disp(F)
to view their outputs.Accessing Single Elements:
Write the code to access the second row and third column of A and display them using disp().
Modifying Elements:
Horizontal Concatenation:
Vertical Concatenation:
Mixed Concatenation:
Now, let’s combine them vertically (on top of each other). (Hint: Use ;)
Example #:
% Create a 3x3 matrix A
A = [1, 2, 3; 2, 4, 6; 0, 0, 0];
% Calculate the rank of matrix A
% Rank represents the maximum number of linearly independent rows/columns
rank_of_A = rank(A);
% Display the rank of matrix A
disp(['The rank of matrix A is: ', num2str(rank_of_A)]);
The most common use of trace in MATLAB is to calculate the sum of the elements on the main diagonal of a square matrix.
Example #:
% Define a square matrix
A = [1 2 3; 4 5 6; 7 8 9];
% Calculate the trace of A (sum of diagonal elements)
trace_of_A = trace(A);
% Display the result
disp(['The trace of A is: ', num2str(trace_of_A)]);
The norm
function in MATLAB calculates the norm of a vector or matrix. The norm represents the magnitude or size of the mathematical object. There are different types of norms depending on the value of a second argument (p) you provide to the function.
Here’s a breakdown of how norm
works in MATLAB:
Syntax:
norm(X)
norm(X, p)
X
is the vector or matrix for which you want to find the norm.p
(optional) specifies the type of norm:
norm
calculates the 2-norm (Euclidean norm) for vectors and the maximum singular value (operator norm) for matrices (which approximates the 2-norm for matrices).Examples:
Important: The Euclidean norm of a square matrix is the square root of the sum of all the squares of the elements.
v = [1 2 3];
vector_norm = norm(v);
disp(['The 2-norm (Euclidean norm) of v is: ', num2str(vector_norm)])
Important: The 1-norm of a square matrix is the maximum of the absolute column sums. [2]
A = [1 2; 3 4];
matrix_norm_1 = norm(A, 1);
disp(['The 1-norm of A is: ', num2str(matrix_norm_1)])
Important: The infinity-norm of a square matrix is the maximum of the absolute row sums.
B = [5 0; -1 2];
matrix_norm_inf = norm(B, Inf);
disp(['The infinity norm of B is: ', num2str(matrix_norm_inf)])
In summary:
norm
calculates the norm of a vector or matrix.See also:
In MATLAB, you can transpose a matrix using two main methods:
Single Quote (‘):
This is the most common and concise way to find the transpose. The single quote symbol (‘) is appended to the matrix name. The transpose operation swaps the rows and columns of the original matrix.
Here’s the syntax:
B = A' % ' denotes transpose
A
is the original matrix.B
is the resulting transposed matrix.transpose
function:
This function offers an alternative way to achieve the transpose. It’s functionally equivalent to the single quote method.
Here’s the syntax:
B = transpose(A)
Important Notes:
B
) containing the transposed elements. They don’t modify the original matrix (A
).Example:
% Define a matrix
A = [1 2 3; 4 5 6];
% Find the transpose using single quote
B = A';
% Find the transpose using transpose function
C = transpose(A);
% Display the original and transposed matrices
disp('Original matrix A:');
disp(A);
disp('Transposed matrix B (using single quote):');
disp(B);
disp('Transposed matrix C (using transpose function):');
disp(C);
This code will output:
Original matrix A:
1 2 3
4 5 6
Transposed matrix B (using single quote):
1 4
2 5
3 6
Transposed matrix C (using transpose function):
1 4
2 5
3 6
As you can see, both methods produce the same transposed matrix.
Example #: Finding the Characteristic Polynomial of a Matrix:
If you provide a square matrix (A), the poly function will return the coefficients of the characteristic polynomial of that matrix. The characteristic polynomial is a polynomial whose roots are the eigenvalues of the matrix.
A = [1 2; 3 4];
p = poly(A);
disp(p); % This will display the coefficients of the characteristic polynomial
The eig
function in MATLAB is a powerful tool for working with eigenvalues and eigenvectors. Here’s a detailed explanation of its functionalities:
What it does:
eig
function computes the eigenvalues and eigenvectors of a square matrix.Inputs:
eig
is a square matrix (A
).Outputs:
eig
can return two or three outputs depending on how you use it:
e = eig(A)
: This returns a column vector containing the eigenvalues of A
.[V, D] = eig(A)
: This returns two matrices:
V
: A matrix whose columns are the eigenvectors of A
. Each column corresponds to an eigenvalue in the diagonal matrix D
.D
: A diagonal matrix containing the eigenvalues of A
on its main diagonal. (The order of eigenvalues in D
corresponds to the columns in V
).Understanding Eigenvalues and Eigenvectors:
Before diving deeper into eig
, it’s crucial to understand eigenvalues (λ) and eigenvectors (v) of a matrix:
Eigenvalue Equation:
Mathematically, this relationship is expressed by the equation Av = λv, where:
eig
Examples:
Here are some examples of how to use eig
in MATLAB:
Finding Eigenvalues:
A = [2 1; 1 3];
e = eig(A);
disp(e); % This will display the eigenvalues of A
Finding Eigenvalues and Eigenvectors:
A = [4 -1; 2 1];
[V, D] = eig(A);
disp(V); % This will display the eigenvectors as columns of V
disp(D); % This will display the eigenvalues on the diagonal of D
See also:
Matrix Creation
A = [1 2 3; 4 5 6];
B = [7 8; 9 10; 11 12];
1. Addition (+):
Important:
A + B is valid if A and B are matrices of the same size.
C_add = A + B;
disp(C_add);
2. Subtraction (-):
Important:
A - B is valid if A and B are matrices of the same size.
C_sub = A - B;
disp(C_sub);
3. Multiplication (*):
Important:
A * B is valid if number of columns of A = number of rows of B.
C_mul = A * B;
disp(C_mul);
4. Exponentiation (^)
Important:
A^2 is valid if A is square and it equals A*A.
C_exp = A^2;
disp(C_exp);
5. Right division (/):
Important:
A/B is valid if A and B are of the same size and is equal to AB^-1^ for the same size square matrices A and B.
C_div_right = A / B;
disp(C_div_right);
6. Left division (\):
Important:
A\B is valid if A and B are of the same size and is equal to A^-1^B for the same size square matrices A and B.
C_div_left = A / B;
disp(C_div_left);
Converting a set of algebraic equations into matrix form (Ax = b) involves representing the system of equations in a more compact and efficient way. Here’s how to do it:
1. Define Your Equations:
Start by writing your set of algebraic equations in a clear and consistent format. Make sure all the variables are defined and used consistently across the equations.
For example, consider a system with two equations and two variables (x and y):
2. Create the Coefficient Matrix (A):
For our example:
A = [ 2 3 ] (row 1: coefficients from equation 1)
[ 1 -1 ] (row 2: coefficients from equation 2)
3. Create the Variables Vector (x):
For our example:
x = [ x ]
[ y ]
4. Create the Constants Vector (b):
For our example:
b = [ 5 ]
[ 1 ]
5. Combine into Matrix Equation (Ax = b):
Finally, the matrix equation representing the system is:
A * x = b
In our example:
[ 2 3 ] * [ x ] = [ 5 ]
[ 1 -1 ] [ y ] [ 1 ]
Generalization:
This process can be extended to any system of linear equations with multiple variables and equations. Just remember to follow the same structure for creating the coefficient matrix, variables vector, and constants vector based on your specific system.
Once you’ve converted your set of algebraic equations into matrix form (Ax = b), there are several methods to solve for the unknown variables (represented by the vector x). Here are three common approaches in MATLAB:
1. Using the \
Operator (Backslash):
The backslash operator (\
) is a convenient way to solve linear systems of equations in MATLAB. It provides a direct solution for x:
% Assuming you have already defined A, b (from your system of equations)
x = A \ b;
% Display the solution vector
disp('Solution (x):')
disp(x)
2. Using the linsolve
Function:
The linsolve
function offers more control over the solution process. It takes two arguments:
% Assuming you have already defined A, b
x = linsolve(A, b);
% Display the solution vector
disp('Solution (x):')
disp(x)
Class Activity:
An example that combines the concepts of creating matrices, performing matrix multiplication, and solving a linear equation system using MATLAB:
Problem Statement:
We have a system of linear equations:
We want to find the values of x and y using MATLAB.
Solution Steps:
Create Matrices:
A = [3 1; 2 -1];
b = [2; 5];
Here, A represents the coefficients of x and y in each equation, and b represents the constant values on the right side.
Solve the System:
\
) operator to solve the system for the variable vector (x). MATLAB will calculate the inverse of A (which is required for solving) and multiply it with b to find x.x = A \ b;
disp(x);
Interpret the Results:
x =
1.0000
2.0000
This indicates that x = 1 and y = 2 satisfy the system of equations.
Explanation:
The backslash operator (\
) is a convenient way to solve linear systems in MATLAB. It performs the following steps behind the scenes:
See also
How to Multiply Matrices - mathsisfun.com
Answer Key (True/False):
How can you access the second element (value 2) of a vector named myVector?
a) myVector(2) Correct b) myVector[2] c) myVector.get(2)
What is the function in MATLAB used to generate a row vector with evenly spaced values between two specified endpoints?
a) linspace Correct b) arange c) logspace
How can you create a 2x3 matrix in MATLAB?
a) matrix = [[1, 2, 3]; [4, 5, 6]] Correct b) matrix = (1, 2, 3; 4, 5, 6) c) matrix = [1 2 3; 4:6]
How can you create a row vector containing 5 evenly spaced values between 2 and 10 in MATLAB?
a) vector = [2:10] b) vector = linspace(2, 10) ✔ c) vector = ones(1, 5) * 2 + 8 d) vector = zeros(1, 5) + 2
What is the output of the following code?
Matlab v = [1 -2 3.5]; element_signs = sign(v); disp(element_signs) Use code with caution. content_copy a) [1 1 1] b) [1 -1 3.5] c) [1 -2 3.5] d) [1 -1 3]
How can you concatenate two matrices A and B horizontally (side-by-side) in MATLAB?
a) C = A + B b) C = A * B c) C = [A, B] ✔ d) C = vertcat(A, B)
Answer Key (Fill in the Blanks):
Concepts:
Matrices:
Matrix Functions:
Additional Questions:
Challenge Questions:
[1] “M.2 Matrix Arithmetic | STAT ONLINE,” PennState: Statistics Online Courses. https://online.stat.psu.edu/statprogram/reviews/matrix-algebra/arithmetic
[2] “Matrix norms,” bathmash.github.io. https://bathmash.github.io/HELM/30_4_mtrx_norms-web/30_4_mtrx_norms-webse1.html (accessed Apr. 16, 2024).
How can you create an evenly spaced row vector without using any built-in functions and explain how the linspace
function works, and provide an example.