Lecture 3
Introduction to Vectors and Matrices
Focus on fundamental aspects of MATLAB for working with vectors and matrices, important for data analysis and engineering problems.
Overview of MATLAB
MATLAB stands for "Matrix Laboratory."
Designed to work efficiently with vectors and matrices.
Mathematical Concepts
Scalars, Vectors, and Matrices
Scalar: A matrix of size 1x1, containing a single element.
Row Vector: Defined as a matrix with a single row and multiple columns (e.g., a 1x4 matrix).
Example:
Row vector representation:
[5, 88, 3, 11] is a 1x4 matrix.
Column Vector: A matrix with multiple rows and a single column (e.g., a 6x1 matrix).
Example:
Column vector representation:
egin{bmatrix} a1 \ a2 \ a3 \ a4 \ a5 \ a6 \ \ \ \ \ \ \ \ \ \ \ \ \ \ \end{bmatrix} where each element corresponds to a different data point.
Matrix: A collection of numbers arranged in a rectangular format; defined by its number of rows and columns.
Example: A 3x4 matrix has 3 rows and 4 columns.
MATLAB Arrays and Vectors Creation
Creating Vectors
A vector can be defined in MATLAB:
Use square brackets, e.g., V = [1, 2, 3, 4] or V = [1 2 3 4] to create a row vector.
To create a column vector, use semicolons between elements:
Example: C = [1; 2; 3; 4] creates a column vector.
Using the Colon Operator
A powerful operator in MATLAB for creating sequences:
Example: 1:5 creates an array of integers from 1 to 5.
Custom increments can be specified:
Example: 1:2:9 generates [1, 3, 5, 7, 9] (from 1 to 9, incrementing by 2).
Using linspace Function
Generates a linearly spaced vector.
Syntax: linspace(X, Y, N) creates a vector of N points between X and Y.
Example: linspace(3, 15, 5) produces the vector [3, 6, 9, 12, 15].
Using logspace Function
Creates logarithmically spaced vectors (not discussed in detail).
Example: logspace(1, 3, 5) gives [10^1, 10^2, 10^3, 10^4, 10^5].
Basic Vector Operations in MATLAB
Concatenating Vectors
Combine vectors:
Example: V = [V1, V2] where V1 and V2 are vectors.
Indexing
Access specific elements using indexes:
vec(i) retrieves the ith element of a vector.
Accessing ranges: vec(a:b) provides elements from index a to b.
Example of non-consecutive indexing:
vec([1, 5, 10]) retrieves the first, fifth, and tenth elements.
Modifying Vector Elements
Changing elements:
V(i) = value changes the value at index i.
Expanding vectors by assigning a value to a new index:
Example: V(6) = 13 adds an element (if none exists, MATLAB fills it with 0 automatically).
Row and Column Vectors
Row vectors can be transposed to become column vectors:
Use the prime operator, e.g., R' (where R is a row vector).
Creating column vectors using semicolon operators is essential in MATLAB.
Creating Matrix Variables
Defining Matrices
Matrices can be created using similar notation as vectors:
Example: M = [4, 3, 1; 2, 5, 6] for a 2x3 matrix.
Accessing Matrix Elements
Access specific values using two indices: M(i,j) where i is the row and j is the column.
Example: M(2,2) retrieves the value in the second row and second column.
Reshaping Matrices
Use the reshape function to change the dimensions of a matrix.
Example: reshape(M, 2, 6) reshapes it into a 2x6 matrix, filled column-wise.
Special Matrices in MATLAB
Creation of Special Matrices
Zero Matrix: Use the function zeros(m,n) to create an m-by-n matrix of zeros.
Ones Matrix: Use the function ones(m,n) to create an m-by-n matrix of ones.
Identity Matrix: Use eye(n) to create an n-by-n identity matrix.
Important Functions for Vectors and Matrices
Length and Size Functions
The function length(vector) returns the number of elements in a vector.
The function size(matrix) returns the number of rows and columns.
The function numel(matrix) returns the total number of elements in the matrix.
Modifying Matrices
Change entire rows/columns:
Example: M(row, :) = [new_values] replaces all elements in a specified row.
Expand matrices by defining a new row or new column:
Specify values for a new row or column corresponding to existing dimensions.
Conclusion and Next Steps
Encouragement to practice and explore further on functions in MATLAB.
Overview of upcoming assignments and study resources.
Emphasis on leveraging MATLAB capabilities for practical engineering analysis and data processing.
Reminder to finish homework and engage with upcoming sessions.