Matrix Operations
## Learning Objectives
- Create and manipulate matrices
- Perform matrix arithmetic
- Apply linear algebra operations
- Use broadcasting for arrays
## Creating Matrices
### Basic Matrix Creation
```matlab
% Direct entry (row separator: ; or newline)
A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
% Using newline instead of semicolon
B = [1, 2, 3
4, 5, 6
7, 8, 9]
```
### Special Matrices
```matlab
zeros(3) % 3x3 matrix of zeros
zeros(3, 4) % 3x4 matrix of zeros
ones(3) % 3x3 matrix of ones
ones(2, 5) % 2x5 matrix of ones
eye(3) % 3x3 identity matrix
eye(3, 4) % 3x4 identity matrix
rand(3) % 3x3 uniform random [0,1]
randn(3) % 3x3 normal distribution
magic(3) % 3x3 magic square
```
### Range-Based Creation
```matlab
% Colon operator
1:5 % [1, 2, 3, 4, 5]
0:0.5:2 % [0, 0.5, 1, 1.5, 2]
10:-1:7 % [10, 9, 8, 7]
% linspace (linear spacing)
linspace(0, 10, 5) % [0, 2.5, 5, 7.5, 10]
% logspace (logarithmic spacing)
logspace(0, 2, 3) % [1, 10, 100]
```
## Matrix Indexing
### Single Element
```matlab
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
A(1, 1) % 1 (row 1, col 1)
A(3, 3) % 9 (row 3, col 3)
A(5) % 5 (linear indexing: column-major)
```
### Submatrices
```matlab
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
A(1, :) % Row 1: [1, 2, 3]
A(:, 2) % Column 2: [2; 5; 8]
A(1:2, 1:2) % Top-left 2x2: [1, 2; 4, 5]
A(end, :) % Last row: [7, 8, 9]
A(:, end) % Last column: [3; 6; 9]
```
### Logical Indexing
```matlab
A = [1, 2, 3, 4, 5];
% Find elements greater than 3
idx = A > 3; % [0, 0, 0, 1, 1]
A(idx) % [4, 5]
% Direct logical indexing
A(A > 3) % [4, 5]
```
### Changing Elements
```matlab
A = [1, 2; 3, 4];
A(1, 1) = 10; % A = [10, 2; 3, 4]
A(2, :) = [5, 6]; % A = [10, 2; 5, 6]
```
## Matrix Arithmetic
### Addition and Subtraction
```matlab
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = A + B % [6, 8; 10, 12]
C = A - B % [-4, -4; -4, -4]
% Scalar operations
C = A + 10 % [11, 12; 13, 14]
```
### Matrix Multiplication
```matlab
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = A * B % Matrix product: [19, 22; 43, 50]
% Element-wise multiplication
C = A .* B % [5, 12; 21, 32]
```
### Matrix Division
```matlab
A = [1, 2; 3, 4];
% Matrix right division (A * inv(B))
C = A / B % A * B^(-1)
% Matrix left division (solves Ax = B)
C = A \ B % A^(-1) * B
% Element-wise division
C = A ./ B % [0.2, 0.333; 0.428, 0.5]
```
### Matrix Power
```matlab
A = [1, 2; 3, 4];
% Matrix power (A * A)
C = A ^ 2 % [7, 10; 15, 22]
% Element-wise power
C = A .^ 2 % [1, 4; 9, 16]
```
## Linear Algebra Operations
### Transpose
```matlab
A = [1, 2, 3; 4, 5, 6];
A' % Transpose: [1, 4; 2, 5; 3, 6]
A.' % Same for real matrices (conjugate transpose for complex)
```
### Determinant and Inverse
```matlab
A = [1, 2; 3, 4];
det(A) % -2
inv(A) % [-2, 1; 1.5, -0.5]
```
### Eigenvalues and Eigenvectors
```matlab
A = [1, 2; 2, 1];
[V, D] = eig(A);
% V: eigenvectors as columns
% D: diagonal matrix of eigenvalues
```
### Matrix Decompositions
```matlab
A = [1, 2; 3, 4];
% LU decomposition
[L, U] = lu(A);
% QR decomposition
[Q, R] = qr(A);
% SVD (Singular Value Decomposition)
[U, S, V] = svd(A);
```
### Solving Linear Systems
```matlab
% Solve Ax = b
A = [1, 2; 3, 4];
b = [5; 11];
x = A \ b % Solution: [1; 2]
x = inv(A) * b % Alternative method
```
## Matrix Manipulation
### Concatenation
```matlab
A = [1, 2];
B = [3, 4];
C = [A, B] % Horizontal: [1, 2, 3, 4]
C = [A; B] % Vertical: [1, 2; 3, 4]
```
### Reshaping
```matlab
A = 1:12;
reshape(A, 3, 4) % 3x4 matrix: [1, 4, 7, 10; 2, 5, 8, 11; 3, 6, 9, 12]
reshape(A, 2, 6) % 2x6 matrix
```
### Repmat and Reshape
```matlab
A = [1, 2; 3, 4];
repmat(A, 2, 3) % Tile A: [1,2,1,2,1,2; 3,4,3,4,3,4; 1,2,1,2,1,2; 3,4,3,4,3,4]
```
### Diagonal and Triangular
```matlab
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
diag(A) % Main diagonal: [1; 5; 9]
diag(A, 1) % First superdiagonal: [2; 6]
diag(A, -1) % First subdiagonal: [4; 8]
triu(A) % Upper triangular
tril(A) % Lower triangular
```
## Broadcasting
### Array-Matrix Operations
```matlab
A = [1, 2, 3;
4, 5, 6];
v = [10, 20, 30]; % Row vector
% Broadcasting: v added to each row
B = A + v % [11, 22, 33; 14, 25, 36]
```
### Array with Column Vector
```matlab
A = [1, 2, 3;
4, 5, 6];
v = [10; 20]; % Column vector
% Broadcasting: v added to each column
B = A + v % [11, 12, 13; 24, 25, 26]
```
### Scalar Broadcasting
```matlab
A = [1, 2, 3;
4, 5, 6];
B = A + 10 % [11, 12, 13; 14, 15, 16]
```
### Element-wise Functions
```matlab
A = [0, pi/2, pi];
sin(A) % [0, 1, 0] - sin applied to each element
exp(A) % [1, 4.81, 23.14]
log(A(2)) % log(pi/2)
```
## Matrix Information
```matlab
A = [1, 2, 3; 4, 5, 6];
size(A) % [2, 3]
length(A) % 3 (largest dimension)
numel(A) % 6
ndims(A) % 2
```
## Summary
- Create matrices with `[row1; row2; ...]` or specialized functions
- Use `:` and indexing `A(row, col)` for access
- `*` is matrix multiplication; `.*` is element-wise
- `'` gives transpose; `^` is matrix power
- Use `inv()`, `det()`, `eig()` for linear algebra
- Broadcasting automatically expands compatible dimensions
- Use `reshape()`, `repmat()`, `diag()` for manipulation
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →