Notes 2
CHAPTER 2: VECTORS AND MATRICES
Overview of MATLAB
MATLAB: abbreviation for "matrix laboratory".
Designed to work superbly with vectors and matrices.
Vectors and Matrices
Definition: Vectors and matrices are structures used to store sets of values that are of the same type.
Examples of Vectors and Matrices:
Scalar (1x1 matrix): 5
Row vector (1x4 matrix): 1 2 3 4
Column vector (6x1 matrix):
\begin{bmatrix} 3.63 \ 7.28 \ 4.59 \ 2 \ 42 \ 19 \end{bmatrix}Matrix (3x4 matrix):
\begin{bmatrix} 5 & 20 & 9.92 & 5.22 \ 98 & 33 & 21 & 24 \ 13 & 24 & 82 & 77 \end{bmatrix}
Creating Row Vectors
Examples to create the same vector
v:Using square brackets:
v = [1 2 3 4]v = [1, 2, 3, 4]Both lines of code will produce:
\text{v} = [1, 2, 3, 4]
Using the Colon Operator and Linspace Function
Colon Operator: Generates a vector with integer steps:
vec = 1:5yields:
\text{vec} = [1, 2, 3, 4, 5]
Specifying step value:
- Syntax:
first:step:lastExample:
v = 1:2:9produces:
\text{v} = [1, 3, 5, 7, 9]v = 9:-2:1gives:
\text{v} = [9, 7, 5, 3, 1]
Decimal values:
Example:
v = 1.1:2.3:8produces:
\text{v} = [1.1, 3.4, 5.7]
Linspace Function
Definition: Creates a linearly spaced vector.
Syntax:
linspace(x, y, n)generatesnvalues in the inclusive range fromxtoy:Example:
v = linspace(3, 15, 5Output:
\text{v} = [3, 6, 9, 12, 15]
Conditions:
x, ycan be integers or decimals, understand thatnmust be a positive integer.
Logspace Function
Definition: Creates a logarithmically spaced vector.
Example:
v = logspace(1, 5, 5)yields:
\text{v} = [10, 100, 1000, 10000, 100000]
Functionality:
logspace(x1, x2, n)generatesnpoints from $10^{x1}$ to $10^{x2}$.
Concatenating Vectors
Definition: Putting two vectors together to create a new vector.
Example:
Vectors
v1 = [-2 0 2 4 6 8]andv2 = [10 3 6 9 12 15]can be concatenated:
\text{newvec} = [v1 \, v2]
Result:
\text{newvec} = [-2, 0, 2, 4, 6, 8, 10, 3, 6, 9, 12, 15]
Referring to and Modifying Vector Elements
Indexing: Elements in a vector are indexed sequentially; MATLAB uses 1-based indexing.
Example of Vector Indexing:
For vector
vec = [1, 3, 5, 7, 9, 3, 6, 9, 12, 15],vec(5)returns:
\text{ans} = 9
Using the Colon Operator: To obtain a subset:
Example:
b = vec(4:6)results in:
\text{b} = [7, 9, 3]
Non-sequential Indices
Example of using an index vector:
c = vec([1, 10, 5])gives:
\text{c} = [1, 15, 9]
Modifying Values in a Vector
To change a value, the index can be specified:
Example:
b(2) = 11results in:
\text{b} = [7, 11, 3]
Vectors can be extended by referring to non-existing indices; gaps will be filled with zeros:
Example:
rv(4) = 2followed byrv(6) = 13results in:
\text{rv} = [3, 55, 11, 2, 0, 13]
Creating Column Vectors
To create a column vector directly:
Use square brackets, separating values with semicolons:
c = [1; 2; 3; 4]Output:
\text{c} = \begin{bmatrix} 1 \ 2 \ 3 \ 4 \end{bmatrix}
Transposing a row vector to a column vector is achieved using the apostrophe:
Example:
r = 1:3; c = r'gives:
\text{c} = \begin{bmatrix} 1 \ 2 \ 3 \end{bmatrix}
Creating Matrix Variables
Matrices can be created explicitly:
Example:
mat = [4 3 1; 2 5 6]results in:
\text{mat} = \begin{bmatrix} 4 & 3 & 1 \ 2 & 5 & 6 \end{bmatrix}
Rule: The number of values in each row must be consistent.
Modifying Matrix Elements
Create and modify a matrix element as follows:
Start matrix:
mat = [2:4; 3:5], results in:
\text{mat} = \begin{bmatrix} 2 & 3 & 4 \ 3 & 4 & 5 \end{bmatrix}Set an element:
mat(2, 3) = 7changes the matrix to:
\text{mat} = \begin{bmatrix} 2 & 3 & 4 \ 3 & 4 & 7 \end{bmatrix}
Changing entire rows: Example:
mat(2,:) = 5:7to change the second row.
Adding Columns and Rows to Matrices
To add a fourth column:
mat(:,4) = [9 2]'transformsmatinto:
\text{mat} = \begin{bmatrix} 2 & 3 & 4 & 9 \ 5 & 6 & 7 & 2 \end{bmatrix}
To add a new third row:
mat(3,:)=[4 7 1 0]changes the matrix accordingly.
Dimensions of Arrays
Length Function:
Returns the number of elements in a vector.
Example: For
vec = -2:1,length(vec)yields:
\text{ans} = 4
Size Function: Returns the number of rows and columns in a matrix:
Example: For
mat = [1:3; 5:7]', the size is:
\text{size(mat)} = [3, 2]
Syntax to retrieve dimensions into variables:
Example:
[r, c] = size(mat)thenr = 3andc = 2.
Numel Function
Returns total number of elements in any array:
For a vector:
numel(vec)gives the count of its entries:
Reshaping Matrices
Reshape Function: Handles changing matrix dimensions.
Example demonstrating a 3x4 reshaping into a 2x6:
Initial matrix
mat = randi(100, 3, 4)yields a 3x4 matrix and can be reshaped:reshape(mat, 2, 6)changes dimensions as specified.
Empty Vectors
To create an empty vector, use
evec = []that outputs:length(evec)results as:
\text{ans} = 0
Adding values to empty vectors can be done via concatenation:
Example operations:
evec = [evec 4]gives:
\text{evec} = [4]evec = [evec 11]results in:
\text{evec} = [4, 11]
Removing Elements from Vectors and Matrices
To delete an element in a vector:
Use indexing:
vec(3) = []removes the element yielding:
\text{vec} = [4, 5, 7, 8]
Subsets can also be removed:
Example:
vec(2:4) = []eliminates specified slice.
For matrices, removing individual elements is impossible; full rows or columns can be deleted:
Example:
mat(:,2) = []yields remaining matrix:
\text{mat} = \begin{bmatrix} 7 & 8 \ 4 & 5 \end{bmatrix}
Three-Dimensional Matrices
Example for creating a 3D matrix:
layerone = reshape(1:15, 3, 5)results in a 3x5 matrix:layertwocan be created as:layertwo = fliplr(flipud(layerone))
Element-wise results can be arranged into a 3D array:
Mathematical Functions with Vectors and Matrices
Finding the sine for every element:
Example:
vec = -2:1gives the vector
\text{sinvec} = sin(vec) yielding:
\text{sinvec} = [-0.9093, -0.8415, 0, 0.8415]
Sum Function: Sums elements of a vector, e.g.
sum(vec1)returns: $\text{ans} = 15$.Product Function: Computes the product of all elements in
vec2yielding:Example:
prod(vec2)results in:
$\text{ans} = 240$.
Min and Max Functions:
Get minimum values using
min(vec1), maximum withmax(vec2), and both with indices:
Example:
[a, b] = min([2, 4, -5, 7]), returns:\text{a} = -5
\text{b} = 3
Working with Random Matrices
Generate random matrices:
Example for random numbers:
A=rand(2)gives an output:
Conclusion on Vectors and Matrices
Understanding how to create, manipulate, and perform operations on vectors and matrices in MATLAB is key for advanced mathematical operations within the environment.