LectureNotes2-24
Chapter 2: Vectors and Matrices
Introduction to MATLAB
MATLAB stands for Matrix Laboratory.
Designed to work effectively with vectors and matrices.
Vectors and Matrices
Definition:
Vectors and matrices store sets of values of the same type.
Example representations:
Scalar: 32 (1x1 matrix)
Row vector: [5 88 3 11] (1x4 matrix)
Column vector: [3.63; 7.28; 4.59] (6x1 matrix)
Matrix:
[ 42 19 23 13 24 82 77 0](3x4 matrix)
Matrix Representation:
Values must align in rows.
Creating Row Vectors
Vector Assignment Examples:
v = [1 2 3 4]v = [1, 2, 3, 4]
Colon Operator:
Generates a vector.
Example:
vec = 1:5Result:
[1 2 3 4 5]
Step Value Usage:
Specify a step size:
v = 1:2:9Result:
[1 3 5 7 9]
Creating Vectors with Decimal Numbers
Decimal Example:
v = 1.1:2.3:8
Using
linspace:Syntax:
v = linspace(x, y, n)Creates a linearly spaced vector with 'n' values from 'x' to 'y'.
Example:
v = linspace(3,15,5)results in[3 6 9 12 15].
Creating Column Vectors
Column Vector Definition:
Defined by placing values in brackets and separating with semicolons.
Example:
c = [1; 2; 3; 4]results in:
1 2 3 4
Modifying Vectors and Elements
Indexing:
Elements are indexed starting at 1.
Example:
vec = [1 3 5 7 9]Access:
vec(5)results in9.
Subsetting Vectors:
Using colon operator:
b = vec(4:6)results in[7 9 3].
Index Vector Example:
c = vec([1 10 5])yields values from specified locations.
Vector Assignment and Extending
Changing Values:
Assigning new values:
b(2) = 11changes the second value.
Extending Vectors:
rv(4) = 2extends the vector.
Handling Gaps:
Gaps will be filled with zeros when extending.
Working with Matrices
Matrix Creation Example:
mat = [4 3 1; 2 5 6]defines a 2x3 matrix.
Random Matrices:
Use
randfor random numbers:A = rand(2).
Creating Identity Matrices:
eye(3)creates a 3x3 identity matrix.
Modifying Matrix Elements
Element Modification:
mat(2,3)=6changes an element.
Row/Column Modification:
Change entire row:
mat(2,:) = 5:7.
Adding Rows and Columns:
mat(:,4) = [9 2]'adds a new column.
Dimensions and Size Properties
Length and Size:
length(vec)gives the number of elements in a vector.size(mat)returns number of rows and columns in a matrix.Use
[r,c] = size(mat)to store results.
Reshaping Matrices
Using
reshape:Changes dimensions without altering data.
Example:
reshape(mat,2,6)results in different dimensional arrangements.
Empty Vectors and Concatenation
Creating Empty Vectors:
evec = []defines an empty vector.
Adding Values to Empty Vectors:
evec = [evec 4]adds values to 'evec'.
Deleting Elements:
Use indexing to remove elements:
vec(3) = [].
Removing Subsets:
E.g.,
vec(2:4) = []removes part of the vector.
Three-Dimensional Matrices
Creating Layers:
Use reshape for layers:
layerone = reshape(1:15,3,5).
Functions with Vectors and Matrices
Basic Functions:
sum,min,maxwork with both vectors and matrices.
Finding Elements:
Use
find()to locate indices meeting criteria, e.g.,find(vec > 5).
Logical Operations
Using Logical Indexing:
Create boolean vectors to filter data.
Functions:
Use
any,all,isequalfor logical conditions within vectors/matrices.
Summary of Operations
Scalar and Array Operations:
Operations can be performed element-wise.
Matrix Multiplication:
Requires matching inner dimensions.
Miscellaneous
Meshgrid Function:
Helps create coordinate grids for functions.
Trace Function:
Returns sum of the diagonal elements of a square matrix.
Extensions and Homework Tips
Experiment with vector and matrix operations, including
dot,cross, and reshaping.Implement logical indexing for data analysis.
Practice creating and manipulating random and structured matrices.