Matlab Note
Matlab Note
BY
What is MATLAB?
History of MATLAB
MATLAB was first developed in the late 1970s by Cleve Moler, a mathematician and computer
scientist. The first version of MATLAB was released in 1984.
Features of MATLAB
1. High-Level Syntax: MATLAB has a simple, intuitive syntax that makes it easy to write
and execute code.
2. Matrix Operations: MATLAB is optimized for matrix operations, making it an ideal tool
for linear algebra, numerical analysis, and data analysis.
3. Built-in Functions: MATLAB has an extensive library of built-in functions for tasks such
as data analysis, visualization, and optimization.
5. Graphics and Visualization: MATLAB has a powerful graphics engine that allows users
to create high-quality 2D and 3D plots, charts, and graphs.
Advantages of MATLAB
1. Easy to Learn: MATLAB has a simple syntax and is relatively easy to learn, even for
users without prior programming experience.
Disadvantages of MATLAB
1. Cost: MATLAB can be expensive, especially for individual users or small organizations.
2. Steep Learning Curve for Advanced Topics: While MATLAB is easy to learn for basic tasks,
advanced topics such as object-oriented programming and parallel computing can have a steep
learning curve.
Applications of MATLAB
1. Numerical Analysis: MATLAB is widely used for numerical analysis, including linear algebra,
differential equations, and optimization.
2. Data Analysis: MATLAB is used for data analysis, including data visualization, statistical
analysis, and machine learning.
3. Signal Processing: MATLAB is used for signal processing, including filtering, convolution, and
Fourier analysis.
4. Image Processing: MATLAB is used for image processing, including image filtering,
segmentation, and recognition.
5. Control Systems: MATLAB is used for control systems, including modeling, simulation, and
control of dynamic systems.
1. Installing MATLAB: Download and install MATLAB from the MathWorks website.
2. Starting MATLAB: Launch MATLAB and familiarize yourself with the interface.
3. Basic Syntax: Learn the basic syntax of MATLAB, including variables, data types, and
operators.
MATLAB Syntax
The term syntax refers to the set of rules that define the structure and arrangement of elements in
a language or system, ensuring they are organized correctly to convey meaning. The concept of
syntax applies in various contexts, including:
Programming: Syntax refers to the rules and structure of a programming language. It defines
how commands, symbols, and statements must be written for the computer to understand
and execute them. MATLAB syntax is designed to work efficiently with matrices, and it includes
various constructs for programming, such as loops, conditional statements, and function
definitions.
Basic MATLAB Syntax Elements
Matrix operations
Matrix operations are a fundamental concept in linear algebra and are used extensively in various
fields such as physics, engineering, computer science, and data analysis.
What is a Matrix?
A matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns.
It is a way to represent a system of equations or a transformation between vectors.
1. Matrix Addition: The sum of two matrices is obtained by adding corresponding elements
of the two matrices.
2. Matrix Multiplication: The product of two matrices is obtained by multiplying the rows
of the first matrix with the columns of the second matrix.
3. Matrix Transpose: The transpose of a matrix is obtained by interchanging its rows and
columns.
4. Matrix Inverse: The inverse of a matrix is a matrix that, when multiplied by the original
matrix, produces the identity matrix.
5. Matrix Determinant: The determinant of a matrix is a scalar value that can be used to
determine the solvability of a system of linear equations.
Matrix Addition
Matrix addition is a simple operation that involves adding corresponding elements of two matrices.
For example, if we have two matrices A and B, the sum of the two matrices is obtained by adding
corresponding elements:
1 2
𝐴=[ ]
3 4
5 6
𝐵=[ ]
7 8
1+5 2+6 6 8
𝐴+𝐵 =[ ]=[ ]
3+7 4+8 10 12
Matrix Determinant
The determinant of a matrix is a scalar value that can be used to determine the solvability of a
system of linear equations. For example, if we have a matrix A, the determinant of A is obtained
by solving the equation det(A) = ad - bc, where a, b, c, and d are the elements of the matrix:
1 2
𝐴=[ ]
3 4
det(𝐴) = 𝑎𝑑 − 𝑏𝑐
det(𝐴) = 1 ∗ 4 − 3 ∗ 2 = −2
Matrix Multiplication
Matrix multiplication is a more complex operation that involves multiplying the rows of the first
matrix with the columns of the second matrix. For example, if we have two matrices A and B, the
product of the two matrices is obtained by multiplying the rows of A with the columns of B:
1 2
𝐴=[ ]
3 4
5 6
𝐵=[ ]
7 8
1∗5+2∗7 1∗6+2∗8 19 22
𝐴∗𝐵 =[ ]=[ ]
3∗5+4∗7 3∗6+4∗8 43 50
Matrix Transpose
The transpose of a matrix is obtained by interchanging its rows and columns. For example, if we
have a matrix A, the transpose of A is obtained by interchanging its rows and columns:
1 2
𝐴=[ ]
3 4
1 3
𝐴^𝑇 = [ ]
2 4
Matrix Inverse
The inverse of a matrix is a matrix that, when multiplied by the original matrix, produces the
identity matrix. For example, if we have a matrix A, the inverse of A is obtained by solving the
equation AX = I, where I is the identity matrix:
MATRICES AND ARRAYS
In MATLAB, matrices and arrays are fundamental data types, and MATLAB is specifically
designed to perform operations on them efficiently. A breakdown of how to work with matrices
and arrays in MATLAB are presented below;
Matrix indexing in MATLAB refers to the methods used to access, modify, or extract elements,
rows, columns, or submatrices from a matrix. MATLAB provides a powerful and flexible way to
work with matrix indices.
what will be the answer if A(6) = 50; % Modify the 6th element in column-major order for the
above example
2. ARRAY OPERATIONS
In MATLAB, array operations allow you to perform various mathematical operations on arrays,
including element-wise operations, matrix operations, and linear algebra functions. Below is an
overview of common array operations and their usage in MATLAB.
1. Element-wise Operations
The transpose of a matrix swaps its rows and columns. In MATLAB, the transpose is denoted by
the single quote (').
2. Matrix Determinant (det)
The determinant of a matrix is a scalar value that can be computed for square matrices. It provides
information about the matrix, such as whether the matrix is invertible (if the determinant is non-
zero).
4. Matrix Trace (trace)
The trace of a matrix is the sum of its diagonal elements. It is often used in various matrix
operations.
The diag function can be used to extract the diagonal elements of a matrix or create a diagonal
matrix from a vector.
4. MATRIX CONCATENATION IN MATLAB
Concatenation is the process of combining two or more matrices (or arrays) into a larger matrix.
In MATLAB, concatenation is a common operation and can be performed along different
dimensions, such as horizontally (side by side) or vertically (stacked on top of each other).
Types of Concatenation
1. Horizontal Concatenation
To concatenate matrices horizontally (side by side), the number of rows must be the same in both
matrices. You use square brackets [] to concatenate them.
In this example, A and B are concatenated along the second dimension (columns), creating a 2x4
matrix.
2. Vertical Concatenation
To concatenate matrices vertically (stacking them on top of each other), the number of columns in
both matrices must be the same. You also use square brackets [] to concatenate them.
In this example, A and B are concatenated along the first dimension (rows), creating a 4x2 matrix.
The two row vectors A and B are concatenated horizontally into a single row vector.
The two column vectors A and B are concatenated vertically into a single column vector.
4. Concatenation with Different Sizes
You can also concatenate matrices of different sizes, as long as they have the same number of rows
(for vertical concatenation) or the same number of columns (for horizontal concatenation).
In this example, A and B are row vectors with different sizes (1x2 and 1x3), but since they are row
vectors, they can be concatenated horizontally.
Concatenate two or more matrices in MATLAB, you can use the cat function or the square bracket
[] operator.
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
(i) Concatenate the matrices horizontally using cat C = cat (2, A, B);
(ii) Concatenate the matrices vertically using cat D = cat (1, A, B);
(iii) Concatenate the matrices horizontally using square bracket E = [A, B];
(iv) Concatenate the matrices vertically using square bracket F = [A; B]
Display the results disp(C); disp(D); disp(E); disp(F);
Output:
disp(C);
1 2 5 6
3 4 7 8
disp(D);
1 2
3 4
5 6
7 8
disp(E);
1 2 5 6
3 4 7 8
disp(F);
1 2
3 4
5 6
7 8
5. RESHAPING AND RESIZING IN MATLAB
Reshaping and resizing are powerful tools in MATLAB for altering the shape or size of matrices
and arrays without changing their elements. These operations allow you to organize data into
different forms suitable for analysis, computation, or visualization.
Reshaping
The reshape function changes the dimensions of a matrix or array to a specified size while
preserving its elements in column-major order (MATLAB's default element storage order).
The elements are taken column-by-column from A to form the new matrix.
Resizing
Resizing involves changing the size of a matrix or array by either adding or removing elements.
6. LOGICAL INDEXING IN MATLAB
Logical indexing is a powerful feature in MATLAB that allows you to select and manipulate
elements of an array or matrix based on conditions. Instead of using numerical indices, logical
indexing uses a logical array of the same size as the target array, where true indicates the elements
to include and false indicates the elements to exclude.
HOW TO WRITE MATLAB PROGRAMS OR CODE
Writing MATLAB programs involves creating scripts or functions that can perform calculations,
process data, or implement algorithms.
Start writing the code in the script file. A MATLAB program typically consists of the following
components
A. Comments
B. Input
D. Mathematical Operations
E. Conditional Statements
G. Functions
• Create reusable functions in separate .m files or as subfunctions.
H. Output
Repmat Function
The repmat function in MATLAB is used to replicate and tile an array or matrix. This function is
particularly useful when you want to create a larger matrix by repeating smaller matrices in a
specific pattern.
The scalar 5 is repeated 3 times in the rows and 4 times in the columns, forming a 3x4 matrix.
The row vector [1, 2, 3] is repeated 4 times vertically to create a 4x3 matrix.
The 2x2 matrix A is replicated 3 times vertically and 2 times horizontally, resulting in a 6x4 matrix.
In this example, the 2x2 matrix A is replicated to form a 4x6 matrix by repeating it 2 times
vertically and 3 times horizontally.
1. Identify the equation: Let's assume a complex equation for r in terms of x, y, and z. For
example: